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

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

Asked by 7 years ago
Edited 7 years ago

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)
0
CharacterAdded is when the player spawns / respawns User#5423 17 — 7y
0
That's what I thought so I wonder why this stops working if it is deleted and then readded? greybird700 63 — 7y
0
Try adding a wait before the objects will be loaded; that's sometimes the problem in these cases. ;/ TheeDeathCaster 2368 — 7y
0
Dang it, Didn't work greybird700 63 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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.

0
Ilysm thank you for helping me greybird700 63 — 7y
Ad
Log in to vote
1
Answered by 7 years ago

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.

Answer this question