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

Help with "Changed"?

I am trying to make some text show the amount of players in game.

NumPlrs = #game.Players:GetChildren()

NumPlrs.Changed:connect(function()
	script.Parent.Text = "There are " ..NumPlrs.. " players playing."
end)

1 answer

✓ Accepted answer

You're basically using an update event on a static variable.

On line one, you get the number from a function that returns a table, meaning you get the number of players in the game. This happens on the first line. If a player joins after the script already ran the first line, nothing updates because the line doesn't get ran more than once.

To fix this problem, use the .Changed event on something that allow it, like an object or service. in this case, a service.

game.Players.Changed:connect(function(property)
	local NumPlrs = #game.Players:GetPlayers()
	script.Parent.Text = "There are " ..NumPlrs.." players playing."
end)

That should work.

The event returns the property that was changed, like player added. You could use an if statement to make sure this was the property that was changed but in my opinion this is just not necessary.

I have tested this with the following script,

--Regular script in ServerScriptService
game.Players.Changed:connect(function(prop)
	print(prop)
	print(#game.Players:GetPlayers())
end)

Output,

  • LocalPlayer

  • 1

Now, it looks like your script is in a LocalScript. I do not think this would change anything, even with FE on.

Good Luck.

4 comments

0

Did not work though ;(

0

I just tested it and it worked for me.

0

That's weird.

0

I know :/

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