How to make a Player Count

never used coroutines before, it works, thanks

1 Like

well i used coroutine but thanks

It’s super easy bro!

while wait() do
script.Parent.Text = "Players: "..#game:GetService("Players"):GetPlayers()
end

that won’t work since it returns a table, it needs to be

while task.wait() do

script.Parent.Text = "Players: "..#game:GetService(“Players”):GetPlayers()

end

Oh yea you’re right I forgot to add the # xD

Please mark this as solved… if any of the above helped you.

local runService = game:GetService("RunService")
local Players = game.Players:GetPlayers()
local txt = script.Parent

runService.RenderStepped:Connect(function()
    txt.Text = "Players: ".. #Players 
end)

please remember click solution*

Yea that could work but that costs a lot of memory, a normal while wait() do or while true do wait() works just fine! Or just a heartbeat

Ya’ll asking him to click solution, you’re answering on a topic that’s over 2 Years old…

2 Likes

:rofl:
someone or @UndercoverAuthor should close this post

1 Like

maybe this can work

local PlayerService = game:GetService("Players")
local GetPlayers = PlayerService:GetPlayers()

txt = script.Parent

local function UpdatePlayers()
	txt = #GetPlayers
end

while wait() do
	UpdatePlayerCount()
	PlayerService.PlayerAdded:Connect(UpdatePlayers)
	PlayerService.PlayerRemoving:Connect(UpdatePlayers)
end
1 Like

Don’t use any of the posts saying to use a while loop to update it, completely unnecessary and inefficient. Listen to when a player is added and removed, like this

local Players = game:GetService("Players")

local function UpdatePlayerCount()
     label.Text = "Player Count: " .. #Players:GetPlayers()
end

UpdatePlayerCount()

Players.PlayedAdded:Connect(UpdatePlayerCount)
Players.PlayerRemoving:Connect(UpdatePlayerCount)
4 Likes

You’re kinda right, it’s good for using less memory

Otherwise just do this

local PlayersService = game:GetService("Players")

PlayersService.PlayerAdded:Connect(function(player)

script.Parent.Text = "Players: "..#PlayersService:GetPlayers()

end)

PlayersService.PlayerRemoving:Connect(function(player)

script.Parent.Text = "Players: "..#PlayersService:GetPlayers()

end)

yeah let me write a new code better

local runService = game:GetService("RunService")
local Players = game.Players:GetPlayers()
local txt = script.Parent

game.Players.ChildAdded:Connect(function()
 txt.Text = "Players: ".. #Players 
end)

game.Players.ChildRemoved:Connect(function()
 txt.Text = "Players: ".. #Players 
end)

please remember click solution*

You forgot about ChildRemoved.

yeah sorry my bad, i change it rn

1 Like