This simple script does not work. It is in a text label and it is a local script.
local label = game.StarterGui.NumPlayerGui.TextLabel -- if I don't access it like this then the error will be tried to index global script a nil value while true do label.Text = "Players In Server: ".. game.Players.NumPlayers wait(1) end
In the properties the text changes but in the game the text does not change. It is weird.
Because NumPlayers is not a value of players, and two, you dont need that long variable. plus, "StarterGui" is a place in bject unto which you put the gui. Its complicated. if you understand hiarchy and such, here is how it works: the objects in the startergui and starterpack containers clone into a new players "Backpack" and "PlayerGui". so the way you have it, it wont change anyway, but if it did, it would change the one in the place nobody sees until they spawn. What you need is the following script:
local label = game.StarterGui.NumPlayerGui.TextLabel--this is you main problem, fixed in the next script while true do label.Text = "Players In Server: ".. game.Players.NumPlayers--"NumPlayers is nonexistent wait(1) end
heres the one you are looking for, and you put this in the label in the screeengui, in the startergui and actually, it should be whenever a new player enters, so we make it a function, not a loop.
local label = script.Parent local NumPlayers = 0 game.Players.PlayerAdded:connect(function() for i, v do in pairs(game.Players:GetChildren()) do NumPlayers = Numplayers+1 end label.Text = "Players In Server: " ..NumPlayers NumPlayers = 0 end)
The reason why it was made a function was because it will work the best, the for loop because it gives an accurate number, and we changed it back to 0 to reset. its all on the iki for a basic outline, as long as you can put two and two together.
This mistake is made a lot. StarterGui is in no way connected to what a player is seeing.
StarterGui is simply a holder that contains useless GUIs that will later be cloned and distributed to individual PlayerGuis.
PlayerGui is located inside each Player, and contains GUI objects that will be visible on that Player's screen.
Each time a Player respawns, PlayerGui will be refreshed. All the GUIs currently in it will be destroyed, and replaced with whatever is currently in StarterGui.
It works similarly to StarterPack; StarterPack is not what is in your Backpack, but contains gear objects that will later be put into your Backpack.
Just put the script inside the TextLabel, then access it through script.Parent
.
local label = script.Parent while true do label.Text = "Players In Server: ".. game.Players.NumPlayers wait(1) end