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

game.Players.Changed is not working?

This is kind of weird. Basically, I have this script that just updates a value to the current number of players in the game

game.Players.Changed:connect(function (NumPlayers)
	System.CurrentPlayers.Value = game.Players.NumPlayers
end)

Now, whenever I do local testing (F5 on keyboard) it updates the value fine. BUT whenever I do local server emulation or whatever it is called (F7 on keyboard) the Value does NOT update properly. Now, if I change my code to this:

game.Players.PlayerAdded:connect(function (NumPlayers)
	System.CurrentPlayers.Value = game.Players.NumPlayers
end)

It updates even during server emulation. HOWEVER, the second variation only updates the player count when someone joins the game, and not when someone leaves. Does anyone have any idea why .Changed is not working? And, even if it does work in a regular game with regular players, it not working in server emulation is a serious problem for me (for when I am debugging alone).

1 answer

The reason why Changed doesn't work? I don't know, how to make the Value of players decrease when players leave? Easy:

game.Players.PlayerAdded:connect(function ()
    System.CurrentPlayers.Value = System.CurrentPlayers.Value+1
end)

game.Players.PlayerRemoved:connect(function ()
	System.CurrentPlayers.Value = System.CurrentPlayers.Value-1
end)

Also, when you were doing your script:

game.Players.PlayerAdded:connect(function (NumPlayers) --Notice the function (NumPlayers)
    System.CurrentPlayers.Value = game.Players.NumPlayers
end)

If you were to do System.CurrentPlayers.Value = NumPlayers the script would error because NumPlayers Would be an object value.

2 comments

0

It says that "PlayerRemoved" is not a valid member of "Players"

0

Do game.Players.PlayerRemoving

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