Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I find how many people are on a team?

Asked by 8 years ago

I want only a certain amount of people to be on a team at once, and I want a variable to have the number of people on that team. So, basically, how do I find how many people are on a team?

This is my attempt at the script:

local Teams = game:GetService("Teams")
 local Team1 = Teams.Host

-- Lets say theres 10 players in Team1, and 8 in Team2..

local amnt = (#Team1:GetChildren())

script.Parent.MouseButton1Down:connect(function()
    if amnt == 0 then
script.Parent.Parent.Parent.Parent.TeamColor = BrickColor.new("Bright red")
end
end)
0
You would want to set up a variable then loop through all players adding on to that variable for every player that is on the specified team. Without an attempt at a script I can not help. M39a9am3R 3210 — 8y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

Objects can only have one parent (and thus only be a child of one object). Player objects are always children of the game.Players service; so you can't use :GetChildren().


As you can see from the Wiki, Team objects don't have any method or property to give you the players on them, so you will have to write it yourself.

A player is on a team if (1) their .Neutral property is false and (2) their .TeamColor is the .TeamColor of the team you want.

You can build the list of all of those players easily using a for loop.

function getPlayersOn(team)
    local list = {}
    for _, player in pairs(game.Players:GetPlayers()) do
        if not player.Neutral and player.TeamColor == team.TeamColor then
            -- `player` is on `team`, so add it to the list on this team:
            table.insert(list, player)
        end
    end
    return list -- return list of players on this team
end



print( #getPlayersOnTeam( Teams.Host ) ) -- # of players on this team will be printed
Ad

Answer this question