I've been doing some research through the wiki, and I found this line of code:
local teams = Game:GetService("Teams"):GetTeams()
After finding this, I want to somehow use this line and make it so you can identify the number of people on each individual team. Say, there were 2 teams. Red and Blue. I want to make it print the number of people on Red, and Blue. I've been experimenting with this a lot, and cannot quite figure it out. Can someone help me out? This is for a game. Much appreciated.
This may sound odd, but you don't even have to use the teams service in this script.
blue = 0 red = 0 for _,v in pairs(game.Players:GetChildren()) do if v.TeamColor.Name == "Bright red" and v.Neutral == false then red = red + 1 elseif v.TeamColor.Name == "Bright blue" and v.Neutral == false then blue = blue + 1 end end print("There are "..red.." players on the red team and "..blue.." players on the blue team.")
As you can see above, I have a script that gets all of the players in the game, and runs that code for each of them. I have values being added to that respond to the team. Also note my if statement saying "and v.Neutral == false". What that does is check if the player is even on a team. If they are neutral, they are ignored. If you want, you can make the script react to players being neutral like so:
blue = 0 red = 0 neutral = 0 for _,v in pairs(game.Players:GetChildren()) do if v.TeamColor.Name == "Bright red" and v.Neutral == false then red = red + 1 elseif v.TeamColor.Name == "Bright blue" and v.Neutral == false then blue = blue + 1 elseif v.Neutral == true then neutral = neutral + 1 end end print("There are "..red.." players on the red team and "..blue.." players on the blue team. "..neutral.." players are neutral.")
There ya go! --XanthicDragon.
A more generic way of doing this is to write a function that returns a table of the players on a specific team. Using this table you can, for instance, teleport everyone on the team named "Blues" to a specific location as well as determine the number of players on the Blues team by using the # operator.
The following function returns tables with player's names instead of userdata.
function getPlayersOnTeam(...) local teams = {} for _, teamName in pairs{...} do local team = game.Teams:FindFirstChild(teamName) if team then local playersOnTeam = {} local teamColor = team.TeamColor for i, player in pairs(game.Players:GetPlayers()) do if player.TeamColor == teamColor then table.insert(playersOnTeam, player.Name) end end table.insert(teams, playersOnTeam) end end return unpack(teams) end
Applying this function:
local blues, reds = getPlayersOnTeam("Blues", "Reds") local numberOfBlues = #blues local numberOfReds = #reds print("There are "..numberOfBlues.." players on Blues and "..numberOfReds.." on Reds") print("The players on Blues include: "..table.concat(blues, ", "))
The previous code should output something like this:
There are 3 players on Blues and 2 players on Reds
The players on Blues include: name, name2, name3
Moving everyone on Blues to a specific location:
local blues = getPlayersOnTeam("Blues") for _, playerName in pairs(blues) do local player = game.Players[playerName] if player.Character then player.Character:MoveTo(Vector3.new(0, 0, 0)) end end