Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

script breaks after you die/reset?

Asked by
Elixcore 1337 Moderation Voter
6 years ago
game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
    repeat wait() until plr.Character:FindFirstChild("Humanoid")
    while wait() do
    if char:FindFirstChild("ForceField") then
                for _,c in pairs(plr.Backpack:GetChildren()) do
                local bp = c:Clone()
                c:Destroy()
                bp.Parent = plr.PlayerGui.tools
                end
                else
                for _,v in pairs(plr.PlayerGui.tools:GetChildren()) do
                local pb = v:Clone()
                v:Destroy()
                pb.Parent = plr.Backpack
                end
        end
    end
    char.Humanoid.Died:connect(function()
                for _,c in pairs(plr.Backpack:GetChildren()) do
                local bbp = c:Clone()
                c:Destroy()
                bbp.Parent = plr.PlayerGui.tools
                end
                end)
end)
end)


--Broken, it spams after u reset.

I made this script, basically what it does is: if you are inside a forcefield, it clones the items from ur inv, puts them in a folder and the deletes the one in ur inventory. if you are outside the forcefield then it puts your items back in ur inv and deletes them from the folder.

The problem is: it works perfectly until you reset/die, once you die and respawn the script starts to spam the items in your inventory like this: https://gyazo.com/59e1b824d31b546f92898e3e789514e2

0
HEY THATS BEEN HAPPENING TO ME TO!! NO WAY!! I NEED THIS ANSWERED TOO. I will try to get your question noticed LeaderAssassinNinja 69 — 6y

1 answer

Log in to vote
1
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

You should use ChildAdded and ChildRemoved instead of loops:

local recordedTools = {}
game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        char.ChildAdded:Connect(function(child)
            if child:IsA("ForceField") then
                local tools = plr.Backpack:GetChildren()
                local equippedTool = char:FindFirstChildOfClass("Tool")
                if equippedTool then
                    table.insert(tools,1,equippedTool)
                end
                recordedTools = tools
                for _,v in pairs(tools) do
                    v.Parent = nil
                end
            end
        end)
        char.ChildRemoved:Connect(function(child)
            if child:IsA("ForceField") and recordedTools then
                for _,v in pairs(recordedTools) do
                    v.Parent = plr.Backpack
                end
            end
        end)
    end)
end)
0
your script doesn't work. Elixcore 1337 — 6y
0
nvm, thank you a lot. the problem is at line 12, you forgot to add "do" thank you a lot dude, it works perfectly Elixcore 1337 — 6y
Ad

Answer this question