【问题标题】:I'm having problems in Roblox Lua script. I want to script it so the inventory is save upon death. Any tips?我在 Roblox Lua 脚本中遇到问题。我想编写脚本,以便在死亡时保存库存。有小费吗?
【发布时间】:2018-12-23 11:09:42
【问题描述】:

我想编写一个脚本,这样当玩家死亡时,物品栏里的东西就不会消失。这是我当前的脚本,关于如何完成此操作的任何指示或提示?我已经将它编写为本地和服务器脚本,它在工作区中,仍然没有运气

local Inventory = {}

local function Spawned(Char)
    local Plr = game.Players:GetPlayerFromCharacter(Char)
    for i,v in pairs(Inventory[Plr]) do
        if Plr['Backpack']:findFirstChild(v.Name) then
            Plr['Backpack'][v.Name]:Destroy()
        end
        v.Parent = Plr['Backpack']  
    end
    Inventory[Plr] = {}
    Char:WaitForChild('Humanoid').Died:connect(function()
        for i,v in pairs({Plr['Backpack'], Char}) do
            for ii,vv in pairs(v:GetChildren()) do
                if vv:IsA('Tool') then
                    table.insert(Inventory[Plr], vv:Clone())
                     vv:Destroy()
                end
            end
         end
     end)
 end

game.Players.PlayerAdded:connect(function(Plr)
    Inventory[Plr] = {}
    local Char = Plr.Character or Plr.CharacterAdded:wait()
    Spawned(Char)
    Plr.CharacterAdded:connect(Spawned)
end)

【问题讨论】:

  • 代码有什么问题?
  • 我的问题是无论我如何更改,当玩家死亡时,物品仍然会消失。这些物品在复制存储中,当玩家购买时,它现在在他的背包和 StarterGear 中,但也许我说得不对。
  • 我想在玩家死亡时,物品栏里的物品留在哪里,而不是消失到玩家必须重新购买它们的地方。

标签: lua roblox


【解决方案1】:

将你想在死亡时坚持的项目放在 Player.StarterGear 中

【讨论】:

  • 我该怎么做?
【解决方案2】:

您可以通过多种不同的方式存储玩家库存。我将在下面展示几个,以便您查看。

1。将武器存放在 StarterGear 中

正如 Coordinate Newton 所说,一旦玩家购买了武器而不是将其仅放在背包中,您还可以将其放入他们的 .StarterGear 文件夹中,这意味着每次他们生成时,工具都会自动放入新背包中.这非常简单,因为您只需要将武器克隆到:

game.Players.LocalPlayer.StarterGear

还有玩家的背包。您在评论中提到您已经尝试过这个,但它没有工作,但我恐怕我想不出它不能正常工作的原因。

2。使用 IntValue 作为全局变量

如果不能使用,那么另一种方法是在每次购买武器时创建一个新的 IntValue 实例,如果武器被移除,则将其销毁。如果您将 IntValue 命名为玩家拥有的武器的名称,那么您可以使用代码在复制存储中查找该武器(如果它存储在那里)。

例如,如果将以下代码放入本地脚本,将创建一个文件夹来存储 IntValues,然后每次玩家重生时,代码将运行所有这些 IntValues,获取名称并将相应的武器放入玩家背包。

game.Players.LocalPlayer.CharacterAdded:Connect(function()
    local Children = Storage:GetChildren()
    if #Children > 0 then --If there are stored weapons
        for _, child in pairs(Children) do
            -- Below: Clone the weapon from Replicated Storage using the name from the IntValue
            local WeaponClone = game.ReplicatedStorage:FindFirstChild(tostring(child.Name)):Clone()
            WeaponClone.Parent = game.Players.LocalPlayer.Backpack
        end
    end
end)

local Storage = Instance.new("Folder") --Folder to store current weapons in
Storage.Name = "BoughtWeapons"
Storage.Parent = game.Player.LocalPlayer

我还创建了两个函数,您可以分别调用它们来创建和销毁 IntValue。

local function WeaponBought(WeaponName)
    local WeaponSave = Instance.new("IntValue")
    WeaponSave.Name = WeaponName
    WeaponSave.Parent = game.Players.LocalPlayer.BoughtWeapons
end

local function WeaponRemoved(WeaponName)
    game.Players.LocalPlayer.BoughtWeapons:FindFirstChild(WeaponName):Destroy()
end

您可以将这两个函数放在任何本地脚本中,它们不必与其他主代码一起使用。您需要做的就是将玩家拥有的武器的名称传递给函数,例如:

WeaponBought("GravityCoil")

这将在文件夹中创建一个名为“GravityCoil”的 IntValue,以在玩家重生时使用。这不像简单地使用 StarterGear 文件夹那么方便,但如果由于某种原因你不能,那么这是一个很好的第二个选择。

此解决方案的一个小问题是,它需要将武器存储在复制存储中,开发者可以很容易地获得它们而无需购买它们。不幸的是,这只是使用本地脚本执行此操作所带来的问题。

您的代码

我很确定您的代码没有按预期工作的原因是,据我所知,玩家的背包在 Humanoid.Died 触发时已经被清除,因此您将无法获取玩家拥有的武器列表。

我希望这对您尝试做的事情有所帮助,即使这不是您所要求的。

【讨论】:

    猜你喜欢
    • 2018-12-01
    • 1970-01-01
    • 2020-03-22
    • 2018-11-20
    • 1970-01-01
    • 2020-07-02
    • 2022-01-09
    • 2021-11-06
    • 2014-01-17
    相关资源
    最近更新 更多