Finding number of people in a team

I am trying to make a script for a round based game where once all the players in a team have died (and changed to a different team), when the team has no one in it, the round ends, however I don’t know how to tell the script how many people are left in the team.

I know that

for i, v in pairs(game:GetService("Teams"):GetTeams()) do
	local players = v:GetPlayers()
	print(#players)
end

will return all players in all teams however is there any way to do something like this but for a specific team?

2 Likes

Here is a helpful article to your problem: https://developer.roblox.com/en-us/api-reference/function/Team/GetPlayers
The code this article provides is:

local teams = game:GetService("Teams"):GetTeams()
for _, team in pairs(teams) do
    local players = team:GetPlayers()
    print("Team " .. team.Name .. " has " .. #players .. " players")
end
2 Likes

Ive looked over that article 5 times over haha, it prints all players in all teams, so if i had 2 teams for example and 2 are in team a and 1 is in team b then it would print
Team a has 2 players
Team b has 1 players
what i want to do is only get the amount of players in only one team, not all

local teams = game:GetService("Teams"):GetTeams()
local amount = 0
for _, team in pairs(teams) do
    local players = team:GetPlayers()
    amount = amount + #players)
end
print(amount)

Tried this script out however it seems to just give a whole number of all players in all teams

To get all players in a specific team, game.Teams.Team:GetPlayers()

 local Team1Players = game:GetService("Teams")["Team1"]:GetPlayers()
 local numberOfPlayersInTeam = #Team1Players

 print("number of players in team = ".. numberOfPlayersInTeam)
 
 --[[ to print every player's name

 for _, player in ipairs(Team1Players) do
        print(player.Name)
 end
   ]]
1 Like

This gets the number of people on a team from team:GetPlayers() for each team.

local Teams = game:GetService("Teams"):GetTeams()

for _, team in pairs(Teams) do
    local playersInTeam = team:GetPlayers()
    print(team.Name.." has "..#playersInTeam.." players on their team")
end

Oh sorry I thought that was what you were asking for

local players = game:GetService("Players"):GetChildren()
local teams = game:GetService("Teams")
local teamName = {}

for _,v in pairs(players) do
    if v.Team = teams["your team name here"] then
       table.insert(teamName,v) -- add a player into the table
    end
end

print(#teamName) -- print the number of players in the team

I think this should work. Kinda long and I don’t think this is the most efficient way.

1 Like

this prints all players in all teams, I’m trying to get the number of all players in one specific team

I’ll give it a try! (30 characters)

If I understand what you’re asking, you can just use GetPlayers in the same way:

local teams = game:GetService("Teams")
local teamName = "Red Team"
local playersInTeam = #teams[teamName]:GetPlayers()

print(playersInTeam)

You were told how multiple times though and rejected all of those? The marked solution at the time of posting uses the Players service’s GetPlayers and filters players out which is less efficient and a reinventing of the wheel for what the Team’s GetPlayers function is for.

local playersOnTeam = game:GetService("Teams")["TeamName"]:GetPlayers()
local numberPlayersOnTeam = #playersOnTeam
15 Likes