How to make a Player Count

Hey scripters,

I’m somewhat new to scripting and would like to know, how do I make a Player Count on a billboard gui text label. Please help in any way possible.

I’ve tried this:

txt = script.Parent

players = game.Players:GetPlayers()

txt.Text = “Players: “…#players…”.”

Thanks!

5 Likes

Try this:

txt = script.Parent
players = game.Players:GetPlayers()

txt.Text = "Players: "..#players.."."
3 Likes

Set it to a game.Players.PlayerAdded and PlayerRemoving event, which will make the screen update every time someone joins or leaves the game.

2 Likes

Where would I put that? (30 characters)

txt = script.Parent
players = game.Players:GetPlayers()

txt.Text = "Players: "..#players.."."

game.Players.PlayerAdded:Connect(function()
    txt.Text = "Players: "..#players.."."
end)
game.Players.PlayerRemoving:Connect(function()
    txt.Text = "Players: "..#players.."."
end)
4 Likes

You would need to do this, I suggest you use my code over @OIogist since it is more organized

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

local function UpdatePlayerCount()
    txt.Text = "Players: ".. #PlayersService:GetPlayers() .."."
end

UpdatePlayerCount()
PlayersService.PlayerAdded:Connect(UpdatePlayerCount)
PlayersService.PlayerRemoving:Connect(UpdatePlayerCount)
4 Likes

it does literally the same thing but sure yours is more organized

3 Likes

Hello there,
You could also try to take a look at the game API.
https://games.roblox.com/docs#!/Games/get_v1_games
Recommended, it’s easier;

and what did i do wrong?

local Players = game:GetService("Players")

playerCount = 0

local function updatePlayerCount(player)
	local players = Players:GetPlayers()
	playerCount = #players
end

while true do
	repeat
		print("players: "..playerCount)
		wait(1)
	until playerCount >= 1
	wait(5)
end

Players.PlayerAdded:Connect(updatePlayerCount)
Players.PlayerRemoving:Connect(updatePlayerCount)

You may have to wrap your while loop in a coroutine because at the look of your code it might be yielding the script preventing the code below to be reached

never used coroutines before, how would i implement it?

Or you could just do this (on a local script)


local Players = game:GetService("Players")

local function updatePlayerCount()
      for i, v in pairs(game.Players:GetPlayers()) do 
             return i
       end
end

game.Players.ChildAdded:Connect(function()
     local count = "Players: " .. updatePlayerCount()
        script.Parent.textLabel.Text = count
end)

game.Players.ChildRemoved:Connect(function()
       local count = "Players: " .. updatePlayerCount()
        script.Parent.textLabel.Text = count
end)

try this, put a script like this:

playercount

and put this into the script:

local PlayerNumber = 0

game.Players.PlayerAdded:Connect(function()
	PlayerNumber = PlayerNumber + 1
	print(PlayerNumber)
	script.Parent.Text = PlayerNumber
end)

game.Players.PlayerRemoving:Connect(function()
	PlayerNumber = PlayerNumber - 1
	print(PlayerNumber)
	script.Parent.Text = PlayerNumber
end)

everytime a player joins the number of the text will get bigger and everytime a player leaves the number will get smaller

if you want to look it like this: Players on server: (number) then replace script.Parent.Text = PlayerNumber with this:

script.Parent.Text = "Players on server:", PlayerNumber

do it on playeradded and playerremoving

oh wait, I didnt notice that this post is a few months old. Oops

This method is not efficient, because what if 2-3 people leave and it takes 1 away, I suggest just using childs & updating them for when you need too as your way is inefficient.

You should loop it through every single player in the game,

for i (index), v (value) in pairs(game.Players:GetPlayers()) do

Index is the number you’re getting, think it as a route so you have like 50 routes inside of 1 folder, you would do.

for i, v in pairs(game.Workspace.Routes:GetChildren()) do
      print("Route"..i) -- It will print the route name
end

So you should loop it through every player inside of the game, this script works on client/server.

local Players = game:GetService("Players")

local function updatePlayerCount()
      for i, v in pairs(game.Players:GetPlayers()) do 
             return i
       end
end

game.Players.ChildAdded:Connect(function()
     local count = "Players: " .. updatePlayerCount()
        script.Parent.textLabel.Text = count
end)

game.Players.ChildRemoved:Connect(function()
       local count = "Players: " .. updatePlayerCount()
        script.Parent.textLabel.Text = count
end)

PS, try not to bump old posts

To count player, just do #game.Players:GetPlayers() and you don’t need to do #players

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

RunService.Stepped:Connect(function()

txt.Text = "Current Players: "..#players:GetPlayers()

end)

-- // Goodluck on your developing!

This should work:

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

PlayerService.PlayerAdded:Connect(function()
txt.Text = "There is "..#count.." players in this server."
end)

PlayerService.PlayerRemovedConnect(function()
txt.Text = "There is "..#count.." players in this server."
end)

PlayerRemoving will fire whenever a player instance leaves the game, even if three players leave at the same time the PlayerRemoving event will fire for each, remember that it’s just firing whenever a player instance is removed from the PlayersService, it’s essentially the same as the following.

game.Players.ChildRemoved:Connect(function(Child)
	--child is a player instance
end)

@evilmen8

A easy way to achieve something like this is to track when players are added and removed from the game and update the billboard accordingly. I’ve wrote some code to show how this could be done:

local Players = game:GetService("Players")

local part = workspace.Part -- The part which the BillboardGui is in

-- Updates the BillboardGui to display the current amount of players in the game
local function updateDisplay()
	part.BillboardGui.TextLabel.Text = "Players: " .. #Players:GetPlayers() 
end

local function onPlayerAdded(player)
	-- Run the updateDisplay function because a player has joined the game
	updateDisplay()
end

local function onPlayerRemoving(player)
	-- Run the updateDisplay function because a player has left the game
	updateDisplay()
end

-- Handle player added
for _, player in ipairs(Players:GetPlayers()) do
	task.spawn(function()
		onPlayerAdded(player)
	end)
end
Players.PlayerAdded:Connect(onPlayerAdded)

-- Handle player removing
Players.PlayerRemoving:Connect(onPlayerRemoving)

When making something like this I’d avoid using while loops or heartbeat loops to track how many players are in the game and opt to use PlayerAdded and PlayerRemoving. It is generally better practice to use events if they exist instead of while loops.

Try this brother

local Players = game:GetService("Players")

playerCount = 0

local function updatePlayerCount(player)
	local players = Players:GetPlayers()
	playerCount = #players
end

coroutine.resume(coroutine.create(function()
 while true do
	repeat
		print("players: "..playerCount)
		wait(1)
	until playerCount >= 1
	wait(5)
  end
end))

Players.PlayerAdded:Connect(updatePlayerCount)
Players.PlayerRemoving:Connect(updatePlayerCount)




1 Like