Number player count not working?

I have this player count GUI script, I’m also using the same function in a workspace and startergui script and local scripts. They used to work before but I just tested it again today in-game (not in-studio) and it doesn’t work. The in-studio testing works (not local servers just pressing the blue play button). So I’m confused why this isn’t working?

I tried game.Players.NumPlayers as well but here’s my code:

txt = script.Parent
local player = game.Players.GetPlayers()
function updateGUI()
		txt.Text = "".. #player .. ""
end
2 Likes

The code you provided will not run at all because you aren’t calling updateGUI(). Additionally, it gets the PlayerList only once (when loading in), and doesn’t get a new list whenever the gui updates. Try this:

local Players = game:GetService("Players")
local TextLabel = script.Parent

function updateGui()
	TextLabel.Text = #Players:GetPlayers()
end

Players.PlayerAdded:Connect(updateGui)
Players.PlayerRemoving:Connect(updateGui)
updateGui()
8 Likes

Umm… it didn’t work in-game. I’m still trying to figure this out.

Any chance you could give us a more detailed explanation of how it isn’t working? Is is doing absolutely nothing, is it not updating correctly, are there any errors?
I know this all may seem very basic, but any extra information will help. This isn’t meant to be insulting, but it’s definitely not disabled and not a server script, right?
Uglypoe’s code works just fine for me, but perhaps you may want to consider switching PlayerRemoving to ChildRemoved to make sure the player object has actually been removed before running any check.

It just doesn’t work, it doesn’t say how many players there are in-game. But in-studio testing it works. I turned on experimental mode and it worked. I’ve checked all the scripts that are being used and they’re all not disabled lol. There are no errors which is frustrating but I’ll try ChildRemoved.

Studio serves as both the client and server at the same time. That is, if you’re play testing solo. If you use the “Clients and Servers” testing under “view” then you can test it as if it was a real game.

Uglypoe’s script as said before works fine for me. However when testing with mine I noticed when testing myself.

  1. If the Gui is a SurfaceGui on a part in the workspace. A Local Script will not change the text in the Gui, switching to a Server Sided script, fixed the problem. That is because LocalScripts only run in the objects listed Here
  2. Basically vice-versa as said above. If the Gui is under PlayerGui, a Server-Sided Script will not change the text in the Gui, it needs to be a local Script. As server-sided scripts only run in the Workspace and ServerScriptService.

Could either of those potentially be your problem?

1 Like

Oh wow I’m dumb, the script in the TextLabel was not a localscript. It works now with a localscript lol.

Thanks!

2 Likes