PlayerAdded Not Firing

So, I’m trying to make a GUI which shows the current player count in the server and it updates when a player joins or leave. It a server script under a TextLabel. However, it not printing or updating when player joins. There is nothing yielding, and I don’t know what wrong.

local function updatePlayers()
	local amountOfPlayers = game:GetService("Players").NumPlayers
	local maxPlayers = game:GetService("Players").MaxPlayers
	
	script.Parent.Text = amountOfPlayers.."/"..maxPlayers
end
	
game:GetService("Players").PlayerAdded:Connect(function()
	print("added")
	updatePlayers()
end)

game:GetService("Players").PlayerRemoving:Connect(function()
	updatePlayers()
end)
1 Like

The Script needs to be a descendant of either the Workspace or ServerScriptService to run. I am assuming your TextLabel is in StarterGui, which server Scripts will not run in.

Instead it is recommended you place a LocalScript under the TextLabel and use RemoteEvents to communicate with a server Script under ServerScriptService.

2 Likes

local PlayerAdded(player)

end

-- init

for _, player in ipairs(Players:GetPlayers()) do
PlayerAdded(player)
end

-- player added

Players.PlayerAdded:Connect(PlayerAdded)
1 Like

Thanks for the information, I didn’t know that it doesn’t fire in StarterGui.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.