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

My death event code only functions once and then after, it stops working.? [SOLVED]

Asked by 3 years ago
Edited 3 years ago

I'm trying to make an item death system where if you die, all the items in your backpack stay the same way they were before you died. So SpeedCoil would be in the first slot if it was in the first slot before, etc.

I already figured out the code and it works, I just can't figure out why it's only running once.. please help!

game.Players.PlayerAdded:Connect(function(plr) -- When player joins in
    wait(1) -- This is so the game doesn't error when there's no humanoid, I know theres WaitForChild btw.
    plr.Character.Humanoid.Died:Connect(function() -- When the player dies
        local items = plr.Backpack:GetChildren() -- Getting the tools in the backpack
        local DeadTools = plr.DeadTools -- A folder to where the tools get stored when player dies
        plr.Character.Humanoid:UnequipTools() -- To prevent tools from being left behind

        for i, item in pairs(items) do
            item.Parent = DeadTools -- Send all tools to the deadtool folder
        end

        print("Player has died, checking if they had any items beforehand..")
        plr.CharacterAdded:Connect(function(chr)
            for i, items in pairs(DeadTools:GetChildren()) do
                items.Parent = plr.Backpack -- Send back to the player when they spawn in
            end
        end)
    end)
end)

1 answer

Log in to vote
0
Answered by 3 years ago

Aha I found out how to fix this by doing a workaround by moving the CharacterAdded event to be first rather than the Death event being second.

Ad

Answer this question