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

Why does this wait permanently disable the script?(unanswered)

Asked by
wjs3456 90
10 years ago

I have this script here that changes you to the orange team once you die. It works but I want to disable it for a few seconds at the beginning of each game. When I add a wait though it permanently disables the script.

game.Players.PlayerAdded:connect(function(p)
p.CharacterAdded:connect(function(c)
c:WaitForChild("Humanoid").Died:connect(function()
p.TeamColor = BrickColor.new("Bright orange") --Or what ever team color it is.
end)
end)
end)

Thanks

0
If you wait, the player might enter before the PlayerAdded event is connected. It's not permanently disabled, it's just disabled for the first person who enters the game. 2eggnog 981 — 10y

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

It would probably be easiest to not use an anonymous function (at least for the PlayerAdded event), then just wait a certain time before connecting the function to the event. So,

function changeTeam(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            player.TeamColor = BrickColor.new("Bright orange")
        end)
    end)
end

wait(50)
game.Players.PlayerAdded:connect(changeTeam)

However, once this is connected, it will stay connected. If you want to be able to stop this script from working whenever you want, you use the disconnect() method.

function changeTeam(player)
    CA = player.CharacterAdded:connect(function(character)
        D = character:WaitForChild("Humanoid").Died:connect(function()
            player.TeamColor = BrickColor.new("Bright orange")
        end)
    end)
end

wait(50)
 PA = game.Players.PlayerAdded:connect(changeTeam)
--stuff
CA:disconnect()
D:disconnect() --Disconnects the events.
PA:disconnect()
0
He's still going to run into the same problem. The player is entering before the PlayerAdded event is connected. 2eggnog 981 — 10y
0
For some reason it says CA is a nil value and it doesn't work wjs3456 90 — 10y
0
Then is there a way to do it egg? wjs3456 90 — 10y
Ad

Answer this question