Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How could I make this script work when a player respawns rather than joins the game?

In my game I want to add this script and then destroy it and readd it but when I do so it no longer works so is there a way for this to activate when the player respawns??

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:Connect(function(Ch)

        local g = script.Arm1:clone()
        g.Parent = Ch
        local C = g:GetChildren()

        for i=1, #C do

            if C[i].className == "Part" then
                local W = Instance.new("Weld")
                W.Part0 = g.Middle
                W.Part1 = C[i]
                local CJ = CFrame.new(g.Middle.Position)
                local C0 = g.Middle.CFrame:inverse()*CJ
                local C1 = C[i].CFrame:inverse()*CJ
                W.C0 = C0
                W.C1 = C1
                W.Parent = g.Middle
            end

            local Y = Instance.new("Weld")
            Y.Part0 = Ch["Left Arm"]
            Y.Part1 = g.Middle
            Y.C0 = CFrame.new(0, 0.4, 0)
            Y.Parent = Y.Part0

        end

        local h = g:GetChildren()
        for i = 1, # h do
            if h[i].className == "Part" then
                h[i].Anchored = false
                h[i].CanCollide = false
            end
        end
    end)
end)

4 comments

0

CharacterAdded is when the player spawns / respawns

0

That's what I thought so I wonder why this stops working if it is deleted and then readded?

0

Try adding a wait before the objects will be loaded; that's sometimes the problem in these cases. ;/

0

Dang it, Didn't work

2 answers

✓ Accepted answer

Loop through the players at the start of the script, so re-adding it won't will still work.

local function characterAdded(Ch)
        local g = script.Arm1:clone()
        g.Parent = Ch
        local C = g:GetChildren()

        for i=1, #C do

            if C[i].className == "Part" then
                local W = Instance.new("Weld")
                W.Part0 = g.Middle
                W.Part1 = C[i]
                local CJ = CFrame.new(g.Middle.Position)
                local C0 = g.Middle.CFrame:inverse()*CJ
                local C1 = C[i].CFrame:inverse()*CJ
                W.C0 = C0
                W.C1 = C1
                W.Parent = g.Middle
            end

            local Y = Instance.new("Weld")
        Y.Part0 = Ch["Left Arm"]
        Y.Part1 = g.Middle
        Y.C0 = CFrame.new(0, 0.4, 0)
        Y.Parent = Y.Part0

    end
    
    local h = g:GetChildren()
    for i = 1, # h do
        if h[i].className == "Part" then
            h[i].Anchored = false
            h[i].CanCollide = false
        end
    end
end

for _,plr in next, game.Players:GetChildren() do
	plr.CharacterAdded:Connect(characterAdded)
end

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:Connect(characterAdded)
end)

NOTE: You should probably not delete the script and re-add it.

1 comment

0

Ilysm thank you for helping me

So if you are adding the script after deleting it, the reason why your script isn't working makes sense.

For the CharacterAdded event to work, you must get a Player first. When your script is first ran, the Player is obtained through the PlayerAdded event, but once that script is deleted and added again, the PlayerAdded event is not ran because the Player is already in the game, which will result in the code not being ran.

My only suggestion is to find a way to run this script without deleting it every time.

This discussion is preserved as it was. New answers, comments, and votes are closed.