How can I get the amount of players on a team?

I want to be able to make a team system so there is nearly equal amounts of players on each team. Similar to arsenal. How can I do this? I have looked everywhere and I cant find a simple enough explanation.

All the best, GalaxyWorld

2 Likes

Sorry for the short topic. [ 30 Characters ]

1 Like
local Teams = game:GetService("Teams")
local Team = Teams.TeamName

function getTeamSize(team)
    return #team:GetPlayers()
end

print(getTeamSize(Team))

Something like this should work.

Note this is only example code.

(Example also only relates to the title, figuring out balancing is up to you or someone else to help)

Roblox also attempt to balance teams themselves. Team | Documentation - Roblox Creator Hub Read the AutoAssignable property description, unless you decide to let players choose their own teams, in which case this might not be helpful to you.

5 Likes

Yea, I’m doing the same thing. XD
You need to learn table(array to do it)
If you know table, then…
There is a property of every each player called “Team”
And just loop and check all the player’s team.

local Players = game.Players:GetPlayers()
local Team = ***Team Name***
local a = 0 --- Amount
for i = 1,#Players do
    if Players[i].Team.Name == Team then
      a = a + 1
    end
end
print(a) --- answer
1 Like

Teams have their own GetPlayers() method, which makes it much simpler than for looping; even if it didn’t exist, you could just use GetChildren() on the Team itself.

1 Like

Yea, I have learned that after I reply it. XD

Also, thanks that you also helps the project that I am working on.

2 Likes

Let’s think about how we would balance all the teams.
For example, if we had 10 players and 2 teams, we would have 5 players in each team.
If we had 9 players and 3 teams, we would have 3 players in each team.

The pattern here is # players / # teams = # players in each team

It would look a bit like this in code:

local players = game:GetService('Players')
local teams = game:GetService('Teams')
local numberOfPlayers = #players
local numberOfTeams = #teams:GetTeams()
local playersInEachTeam = numberOfPlayers / numberOfTeams

function BalanceTeams()
    local teamNum = 1
    for i = 1, players:GetPlayers(), 1 do
          teamNum = math.ceil(i / playersInEachTeam)
          local p = players:GetPlayers()[i]
          p.Team = teams:GetTeams()[teamNum]
    end
end

Hope this helped! If I made any mistakes, please tell me.

Edit: The BalanceTeams() function basically goes through all players, and each time it reaches the number of players in each team it increments the teamNum, and sets the corresponding team to the player.

2 Likes

Testing all of the solutions now.