How to check if there are a certain amount of players

It has been a while since I have scripted so i’m a bit rusty.

How do I check if there are over 2 players in the game.

6 Likes

You can check by getting children in game.Players:

players = game.Players:GetChildren()
if #players >= 2 then
--stuff
end
18 Likes

Adding onto this, use Players:GetPlayers(). This creates a table that only returns player objects and ignores any other instance that is inexplicably sitting in Players.

9 Likes

It works thanks! Really appreciated.

1 Like

You can get all the players, and the n get the amount from that.

Additionally, make it a function so you dont need to manually update it every time it changes.

local players = game:GetService([[Players]])
--The bracketed Players is a string. Sorry, I can not use quotes on mobile
local function getNumberOfPlayers()
    return #players:GetPlayers()
end

print(getNumberOfPlayers())
3 Likes

It’s not just that GetPlayers functions in relatively the same way as GetChildren except only for Player objects, but this is also the canonical way to fetch a list of players in the game.

3 Likes