Teams offer a range of features that are useful to developers. These can broadly be divided into features that work out-of-the-box and features developers can program into their game.
Built-in team behavior The following functionality of Teams exists by default and does not require the developer to program any custom behavior.
- When using the default player list users will be grouped and displayed by team
Optional extended team behavior Many developers chose to add the following features to teams in their own code.
- Implement checks for team in weapon code to prevent team killing
- Implement doors or other features that only certain teams can use
- Periodically reassign teams to maintain team balance
local Teams = game:GetService("Teams")-- create two teamslocal redTeam = Instance.new("Team")redTeam.TeamColor = BrickColor.new("Bright red")redTeam.AutoAssignable = trueredTeam.Name = "Red Team"redTeam.Parent = Teamslocal blueTeam = Instance.new("Team")blueTeam.TeamColor = BrickColor.new("Bright blue")blueTeam.AutoAssignable = trueblueTeam.Name = "Blue Team"blueTeam.Parent = Teams-- start counting the number of players on each teamlocal numberRed, numberBlue = 0, 0local function playerAdded(team)-- increase the team's count by 1if team == redTeam thennumberRed = numberRed + 1elseif team == blueTeam thennumberBlue = numberBlue + 1endendlocal function playerRemoved(team)-- decrease the team's count by 1if team == redTeam thennumberRed = numberRed - 1elseif team == blueTeam thennumberBlue = numberBlue - 1end-- check if the teams are unbalancedlocal bigTeam, smallTeam = nil, nilif (numberRed - numberBlue) > 2 thenbigTeam = redTeamsmallTeam = blueTeamelseif (numberBlue - numberRed) > 2 thenbigTeam = blueTeamsmallTeam = redTeamendif bigTeam then-- pick a random playerlocal playerList = bigTeam:GetPlayers()local player = playerList[math.random(1, #playerList)]-- check the player existsif player then-- change the player's teamplayer.TeamColor = smallTeam.TeamColor-- respawn the playerplayer:LoadCharacter()endendend-- listen for players being added / removedblueTeam.PlayerAdded:Connect(function(_player)playerAdded(blueTeam)end)blueTeam.PlayerRemoved:Connect(function(_player)playerRemoved(blueTeam)end)redTeam.PlayerAdded:Connect(function(_player)playerAdded(redTeam)end)redTeam.PlayerRemoved:Connect(function(_player)playerRemoved(redTeam)end)
local Players = game:GetService("Players")local door = Instance.new("Part")door.Anchored = truedoor.Size = Vector3.new(7, 10, 1)door.Position = Vector3.new(0, 5, 0)door.Parent = workspacelocal debounce = falsedoor.Touched:Connect(function(hit)if not debounce thendebounce = trueif hit thenlocal player = Players:GetPlayerFromCharacter(hit.Parent)if player and player.TeamColor == BrickColor.new("Bright red") thendoor.Transparency = 0.5door.CanCollide = falsetask.wait(3)door.Transparency = 0door.CanCollide = trueendendtask.wait(0.5)debounce = falseendend)
local Players = game:GetService("Players")function checkTeamKill(playerAttack, playerVictim)if playerAttack.Team ~= playerVictim.Team or playerAttack.Neutral or playerVictim.Neutral thenreturn falseendreturn trueendlocal players = Players:GetPlayers()checkTeamKill(players, players)
The GetTeam function returns a table containing the game's Team objects.
Note this will only return Team objects that are directly parented to the Teams service. For this reason it is recommended developers only parent Team objects to the Teams service and not to other Instances (or to each other).
local Teams = game:GetService("Teams")local teams = Teams:GetTeams()for _, team in pairs(teams) dolocal players = team:GetPlayers()print("Team", team.Name, "has", #players, "players")end