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
1 comment
Thank you. I'll look over this carefully. +1 rep.