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?
local teams = game:GetService("Teams"):GetTeams()
for _, team in pairs(teams) do
local players = team:GetPlayers()
print("Team " .. team.Name .. " has " .. #players .. " players")
end
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)
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
]]
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
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.
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