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)
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.
local PlayerAdded(player)
end
-- init
for _, player in ipairs(Players:GetPlayers()) do
PlayerAdded(player)
end
-- player added
Players.PlayerAdded:Connect(PlayerAdded)