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

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

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

1 comment

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.

1 answer

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()

3 comments

0

He's still going to run into the same problem. The player is entering before the PlayerAdded event is connected.

0

For some reason it says CA is a nil value and it doesn't work

0

Then is there a way to do it egg?

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