How to see how many players are ingame (Scripting guide)

Do you want to see how many players are in your game? This can be used in lots of stuff and is very important.

Firstly, We will be getting the players characters, This is what the game will count.

Heres the code.

local children = game.workspace:GetChildren()

Just a variable to find all characters.

Secondly… We will be finding how many players are in the game.

Here’s that code:

while wait(0.05) do -- Optional loop script (Checks for players every ,. seconds)
    if #children > 5 then -- If a certain amount of players are higher then the number
       print("Enough players!") -- Reward / Consequence
  else
       print("Not enough players!")
    end
end)

So, every ,. seconds the game will check if there are enough players, if there is enough.
the game will print, “Enough Players!” if there is not, it will print, not enough players!

Hope that helped!

6 Likes

A more useful and efficient method for collecting players in-game would be to use the :GetPlayers() method. This will return a table of all connected player objects in your game. It functions the same way as Instance:GetChildren() except it returns player objects.

I’m going to show you some examples that use :GetPlayers().

First useful example:

local PlayersService = game:GetService("Players")

task.wait(5)

for _, Player in pairs(PlayersService:GetPlayers()) do -- Iterates over a table containing all the player objects in-game
	print(Player.Name) -- Prints out the player object name (Bob, Lily, etc.)
end

Second useful example:

local PlayersService = game:GetService("Players")

repeat task.wait(2) until #PlayersService:GetPlayers() >= 2 -- Repeats every 2 seconds until there are two or more players in the game before starting the next lines of code

print("Match starting...")

If you’d like to read up more on :GetPlayers() or view more coding examples, here is the link to the documentation page: Players | Roblox Creator Documentation

I hope this helps.

16 Likes

First problem,

This is a constant variable, you never update it plus if you would have multiple children directly under workspace this would not work. The only reason this seemed to work is because you tested it alone and in a baseplate. After which the script loads before your character is parented to workspace.

Instead I suggest to do something like what @EnchanteBruh did, though instead I suggest to replace:

With something along the lines of:

repeat PlayersService.PlayerAdded:Wait() until #PlayersService:GetPlayers() >= 2

Instead of checking every 2 seconds it checks every time a player has joined.

After that statement you can put any code you would like, but it would wait until a new player has joined.
Another thing you could do is when you have too many players

if #PlayersService:GetPlayers() >= 10 then
   -- too many players
   repeat PlayersService.PlayerRemoving:Wait() until #PlayersService:GetPlayers() <= 8
end
4 Likes

…or just use #PlayersService:GetPlayers() and that will give you the amount of players in game

3 Likes
while task.wait(0.05) do
   local children = game:GetService("Players"):GetPlayers()
   if #children > 5 then
       print("Enough players!")
   else
       print("Not enough players!")
   end
end
3 Likes

Just because you can post a tutorial, it doesn’t mean you should by the way.

The code is completely wrong.

By getting the workspace’s children, you are getting EVERY other instance and model besides the characters. That will never return the correct number. Use game.Players:GetPlayers() instead.

Running a while loop so frequently is terrible for optimization. Realistically you could just run the loop once every 1-5-10 seconds, depending on how accurate you need the number to be at any given time. Doing it every 0.05s is unnecessary. Also, wait() should be replaced with task.wait() since wait() is semi-deprecated.

Lastly, you are getting the number of players (or, rather, every instance in the workspace in this case) only once at the start of the script. Meaning the code will always return the same value and not the live number of players. So the whole thing is just pointless. You should use :GetChildren() inside the loop.

Delete it bald!

4 Likes

What? I suggest if you make a tutorial you ensure it works. I’ll list all the problems and I will give the solution.

The function :GetChildren() will get all parts within an object. It will return them from a table. As in all parts, I mean the first objects under the given parent. In this case it will be workspace, so it will return all the objects within workspace. Be aware it wont return objects under the given objects.

This could come in use if you got all the player models, but it is not the most efficient way to do so.

Solution:

If nothing except players were in the folder “Players”

local Players = #game.Players:GetChildren() -- returns the amount of players

But the most efficient way I know how to do it is this way:

local Players = {}
local Player_Amount = 0

for a,b in pairs(game.Players:GetChildren()) do
    if b:IsA("Player") then
       table.insert(Players, b)
    end
end)

Player_Amount = table.maxn(Players)

So I suggest you take this post down or edit it. This is just me though.

That’s just a glorified #game.Players:GetPlayers()

1 Like

Yep. I forgot about that function. If I was scripting I would think about it but I am on mobile right now.

Aren’t you the same person who made this topic???