如果玩家死亡,但有同一工具的克隆,而不是所有克隆只保存1只有1个,我遇到问题,我该如何解决此问题?

发布于 2025-02-13 14:08:55 字数 1000 浏览 0 评论 0原文

我的最终成就是要在玩家死亡时要保存的重复工具

,如果玩家拥有三个“枪1”,那么死亡而不是在库存中仍然有三枪1,他们只有一个枪1。

这就是我目前有

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)

My end achievement is wanting the duplicate tools to save when the player dies

For example, if the player owns three "Gun 1s" then dies instead of still having three Gun 1s in their inventory they will only have one Gun 1.

This is what I currently got

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌入人心 2025-02-20 14:08:56

您在这里摧毁克隆。

if Plr['Backpack']:findFirstChild(v.Name) then
   Plr['Backpack'][v.Name]:Destroy()
end

You are destroying the clones here.

if Plr['Backpack']:findFirstChild(v.Name) then
   Plr['Backpack'][v.Name]:Destroy()
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文