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)
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