Teams

Show Deprecated
Not Creatable
Service

The Teams service holds a game's Team objects. Team objects must be parented to the Teams service.

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.

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

Code Samples

Simple Team Rebalance

1local Teams = game:GetService("Teams")
2
3-- create two teams
4local redTeam = Instance.new("Team")
5redTeam.TeamColor = BrickColor.new("Bright red")
6redTeam.AutoAssignable = true
7redTeam.Name = "Red Team"
8redTeam.Parent = Teams
9
10local blueTeam = Instance.new("Team")
11blueTeam.TeamColor = BrickColor.new("Bright blue")
12blueTeam.AutoAssignable = true
13blueTeam.Name = "Blue Team"
14blueTeam.Parent = Teams
15
16-- start counting the number of players on each team
17local numberRed, numberBlue = 0, 0
18
19local function playerAdded(team)
20 -- increase the team's count by 1
21 if team == redTeam then
22 numberRed = numberRed + 1
23 elseif team == blueTeam then
24 numberBlue = numberBlue + 1
25 end
26end
27
28local function playerRemoved(team)
29 -- decrease the team's count by 1
30 if team == redTeam then
31 numberRed = numberRed - 1
32 elseif team == blueTeam then
33 numberBlue = numberBlue - 1
34 end
35
36 -- check if the teams are unbalanced
37 local bigTeam, smallTeam = nil, nil
38 if (numberRed - numberBlue) > 2 then
39 bigTeam = redTeam
40 smallTeam = blueTeam
41 elseif (numberBlue - numberRed) > 2 then
42 bigTeam = blueTeam
43 smallTeam = redTeam
44 end
45
46 if bigTeam then
47 -- pick a random player
48 local playerList = bigTeam:GetPlayers()
49 local player = playerList[math.random(1, #playerList)]
50
51 -- check the player exists
52 if player then
53 -- change the player's team
54 player.TeamColor = smallTeam.TeamColor
55 -- respawn the player
56 player:LoadCharacter()
57 end
58 end
59end
60
61-- listen for players being added / removed
62blueTeam.PlayerAdded:Connect(function(_player)
63 playerAdded(blueTeam)
64end)
65
66blueTeam.PlayerRemoved:Connect(function(_player)
67 playerRemoved(blueTeam)
68end)
69
70redTeam.PlayerAdded:Connect(function(_player)
71 playerAdded(redTeam)
72end)
73
74redTeam.PlayerRemoved:Connect(function(_player)
75 playerRemoved(redTeam)
76end)
Team Kill Check

1local Players = game:GetService("Players")
2
3function checkTeamKill(playerAttack, playerVictim)
4 if playerAttack.Team ~= playerVictim.Team or playerAttack.Neutral or playerVictim.Neutral then
5 return false
6 end
7 return true
8end
9
10local players = Players:GetPlayers()
11
12checkTeamKill(players[1], players[2])
Team Only Door

1local Players = game:GetService("Players")
2
3local door = Instance.new("Part")
4door.Anchored = true
5door.Size = Vector3.new(7, 10, 1)
6door.Position = Vector3.new(0, 5, 0)
7door.Parent = workspace
8
9local debounce = false
10
11door.Touched:Connect(function(hit)
12 if not debounce then
13 debounce = true
14 if hit then
15 local player = Players:GetPlayerFromCharacter(hit.Parent)
16 if player and player.TeamColor == BrickColor.new("Bright red") then
17 door.Transparency = 0.5
18 door.CanCollide = false
19 task.wait(3)
20 door.Transparency = 0
21 door.CanCollide = true
22 end
23 end
24 task.wait(0.5)
25 debounce = false
26 end
27end)

Summary

Properties

Events

Methods


Returns a table containing the game's Team objects. Will only return Team objects that are parented to the Teams service.

Properties

Events

Methods

GetTeams

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


Returns

An array of Teams in the game.

Code Samples

Teams GetTeams

1local Teams = game:GetService("Teams")
2
3local teams = Teams:GetTeams()
4
5for _, team in pairs(teams) do
6 local players = team:GetPlayers()
7 print("Team", team.Name, "has", #players, "players")
8end