How would I get a count of how many players there are in a game?

So basically I have a teams script and I need it to count how many players there are in a game, so it can determine the amount of teams. Here is my script:

 ''''
        repeat wait() until #game.Players:GetPlayers()>= --idk
print("1")
local Teams = game:GetService("Teams")

local PlayerList = game.Players:GetPlayers()

local TeamColors = {
				BrickColor.Red(),
				BrickColor.Blue(), 
				BrickColor.Green(), 
				BrickColor.White(),
                BrickColor.Black()
			   } --add based on the max num of teams in your game
print("2")
local TeamColorCount = 1

for Counter = 1, #PlayerList, 2 do
local Team = Instance.new("Team")
print("3")
local Player1, Player2 = PlayerList[Counter], PlayerList[Counter+1]
print("4")

-- creating the team
Team.TeamColor = TeamColors[TeamColorCount]
print("5")
Team.Parent = Teams
print("6")

-- adding players to team
Player1.Team = Team
print("7")
--checking that there is a second player on the team to add
if Player2 then
print("8")
	Player2.Team = Team
print("9")
end
TeamColorCount = TeamColorCount + 1
print("10")
end


    '''
1 Like

What do you think that is doing…? The # part.

I also see you had a lot of this solved in your other thread.

2 Likes

Even when I remove this line:

Still doesn’t work

#PlayerList, PlayerList:len() both get the length of :GetPlayers()

I have no idea what “doesn’t work”, what should happen, or what you want to happen.

I’m not sure if you meant to reply to me with that,but I’m fully aware. Thanks.

Sorry, meant to reply to the post @MrNicNac

1 Like

FYI: Tables don’t have a len method, infact they don’t have any at all, you’re thinking of strings.

1 Like

That sounds fairly simple.

print(#game:GetService('Players'):GetPlayers())

By using the length operator, we can easily know how many players are in the game.

You Could set a variable to it and Update it at the end of something like a Countdown
Use what Xueify Posted and use if then on the Countdown to when its 0 and update the var , sorry for my bad explanation.

> Xueify : print(#game:GetService('Players'):GetPlayers())

If you want to get the amount of players in a game you can do:

local playernum = #game:GetService("Players"):GetChildren()

You can do this to get the amount of players currently on the server.

-- get the players service
local players = game:GetService("Players")
-- # gets the amount of stuff in a table but it could also work with GetChildren() and GetPlayers()
local count = #players:GetPlayers()

Sorry for bad explaining before, but I’m trying to make it so that the the teams script work on any amount of player, because right now it doesn’t… @Xueify @Hurriedcarrot62 @j8cksxn @MrNicNac

You could use:
local amtofplr = 0
for I, v in pairs(game.Players:GetPlayers()) do
amtofplr = amtofplr + 1
end

Where in my script would I put this?

Maybe read this…

Where in my script would you put the code in? (Also it is a round script I’m making and it will repeat after it loops again)

Wherever you want to start counting the players you could also make it a function then.

Which is at the beginning, and it doesn’t work, and no errors, but it doesn’t print.

   ''''
local amtofplr = 0
for i, v in pairs(game.Players:GetPlayers()) do
amtofplr = amtofplr + 1
print("Player")
end
print("1")
local Teams = game:GetService("Teams")

local PlayerList = game.Players:GetPlayers()

local TeamColors = {
				BrickColor.Red(),
				BrickColor.Blue(), 
				BrickColor.Green(), 
				BrickColor.White(),
                BrickColor.Black()
			   } --add based on the max num of teams in your game
print("2")
local TeamColorCount = 1

for Counter = 1, #PlayerList, 2 do
local Team = Instance.new("Team")
print("3")
local Player1, Player2 = PlayerList[Counter], PlayerList[Counter+1]
print("4")

-- creating the team
Team.TeamColor = TeamColors[TeamColorCount]
print("5")
Team.Parent = Teams
print("6")

-- adding players to team
Player1.Team = Team
print("7")
--checking that there is a second player on the team to add
if Player2 then
print("8")
	Player2.Team = Team
print("9")
end
TeamColorCount = TeamColorCount + 1
print("10")
end
        '''

You need to use: local PlayerNumber = game.Players:GetChildren()

Are you talking about the current script?