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 behaviour The following functionality of Teams exists by default and does not require the developer to program any custom behaviour.
- 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
1local Teams = game:GetService("Teams")23-- create two teams4local redTeam = Instance.new("Team")5redTeam.TeamColor = BrickColor.new("Bright red")6redTeam.AutoAssignable = true7redTeam.Name = "Red Team"8redTeam.Parent = Teams910local blueTeam = Instance.new("Team")11blueTeam.TeamColor = BrickColor.new("Bright blue")12blueTeam.AutoAssignable = true13blueTeam.Name = "Blue Team"14blueTeam.Parent = Teams1516-- start counting the number of players on each team17local numberRed, numberBlue = 0, 01819local function playerAdded(team)20 -- increase the team's count by 121 if team == redTeam then22 numberRed = numberRed + 123 elseif team == blueTeam then24 numberBlue = numberBlue + 125 end26end2728local function playerRemoved(team)29 -- decrease the team's count by 130 if team == redTeam then31 numberRed = numberRed - 132 elseif team == blueTeam then33 numberBlue = numberBlue - 134 end3536 -- check if the teams are unbalanced37 local bigTeam, smallTeam = nil, nil38 if (numberRed - numberBlue) > 2 then39 bigTeam = redTeam40 smallTeam = blueTeam41 elseif (numberBlue - numberRed) > 2 then42 bigTeam = blueTeam43 smallTeam = redTeam44 end4546 if bigTeam then47 -- pick a random player48 local playerList = bigTeam:GetPlayers()49 local player = playerList[math.random(1, #playerList)]5051 -- check the player exists52 if player then53 -- change the player's team54 player.TeamColor = smallTeam.TeamColor55 -- respawn the player56 player:LoadCharacter()57 end58 end59end6061-- listen for players being added / removed62blueTeam.PlayerAdded:Connect(function(_player)63 playerAdded(blueTeam)64end)6566blueTeam.PlayerRemoved:Connect(function(_player)67 playerRemoved(blueTeam)68end)6970redTeam.PlayerAdded:Connect(function(_player)71 playerAdded(redTeam)72end)7374redTeam.PlayerRemoved:Connect(function(_player)75 playerRemoved(redTeam)76end)
1local Players = game:GetService("Players")23function checkTeamKill(playerAttack, playerVictim)4 if playerAttack.Team ~= playerVictim.Team or playerAttack.Neutral or playerVictim.Neutral then5 return false6 end7 return true8end910local players = Players:GetPlayers()1112checkTeamKill(players, players)
1local Players = game:GetService("Players")23local door = Instance.new("Part")4door.Anchored = true5door.Size = Vector3.new(7, 10, 1)6door.Position = Vector3.new(0, 5, 0)7door.Parent = workspace89local debounce = false1011door.Touched:Connect(function(hit)12 if not debounce then13 debounce = true14 if hit then15 local player = Players:GetPlayerFromCharacter(hit.Parent)16 if player and player.TeamColor == BrickColor.new("Bright red") then17 door.Transparency = 0.518 door.CanCollide = false19 task.wait(3)20 door.Transparency = 021 door.CanCollide = true22 end23 end24 task.wait(0.5)25 debounce = false26 end27end)
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).
1local Teams = game:GetService("Teams")23local teams = Teams:GetTeams()45for _, team in pairs(teams) do6 local players = team:GetPlayers()7 print("Team", team.Name, "has", #players, "players")8end