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.
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.
You can check by getting children in game.Players:
players = game.Players:GetChildren()
if #players >= 2 then
--stuff
end
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
.
It works thanks! Really appreciated.
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())
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.