I recommend adding a wait statement, have you also tried a for i,v statement, and having a value to keep the number of players there? What I just said isn’t efficient, but if what you just did didn’t work, I recommend to try new methods, or keep trying to get what you listed to work.
When you store something in a variable it keeps a reference to that value. It does not update on it’s own.
Here is an example of something you could do:
local Players = game:GetService("Players")
local NumPlayers = Players.NumPlayers -- Will always be the initial NumPlayers value (most likely 0 unless the script ran after you joined) unless set again
someCallbackOrEvent:Connect(function() -- Could be at any time
print(Players.NumPlayers) -- Will always be up to date.
end)
I would also recommend not using wait to fix your problems. If you’re using wait anywhere besides permanent game loops (e.g. game ticks) or places where timing is involved (e.g. round timers) it’s probably a sign you’re not doing something the way it should be done.
(I’d recommend using game:GetService(“Players”) btw)
Why store it in a variable at all? And if you really want a variable you can connect events to Players.PlayerAdded and Players.PlayerRemoving which will update your variable.
#game.Players:GetPlayers() will always return the current amount of players in the game. If the variable you assigned to it is returning 0 and there are people in the game, then your code’s logic must be messing up somewhere.