PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Team

Player

Team

The Team property is a reference to a Team object within the Teams service. It determines the team the player is on; if the Player isn’t on a team or has an invalid Player/TeamColor, this property is nil. When this property is set, the player has joined the Team and the Team/PlayerAdded event fires on the associated team. Similarly, Team/PlayerRemoved fires when the property is unset from a certain Team.


Code Samples


Join Team Command

This code sample allows any player to chat "/jointeam " where is the name of a team. It uses string manipulation using string.sub and string.lower to make the command case-insensitive and allow for partial matches. For example, “/jointeam red” will match the team “Red Robins”.

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

-- Command to choose a team (note the trailing space)
local joinCommand = "/jointeam "

local function findTeamByName(name)
	-- First, check for the exact name of a team
	if Teams:FindFirstChild(name) then return Teams[name] end
	-- Let's check for case-insensitive partial matches, like "red" for "Red Robins"
	for _, team in pairs(Teams:GetChildren()) do
		if team.Name:sub(1, name:len()):lower() == name:lower() then
			return team
		end
	end
	-- If we get to this point, no team matched the one we were looking for :(
end

local function onPlayerChatted(player, message, recipient)
	-- Note: string.sub(message, ...) is the same as message:sub(...)
	if message:sub(1, joinCommand:len()):lower() == joinCommand:lower() then
		-- Matched "/JOINTEAM xyz" to our join command prefix "/jointeam "
		local teamName = message:sub(joinCommand:len() + 1) -- Cut out the "xyz" from "/jointeam xyz"
		local team = findTeamByName(teamName)
		if team then
			-- Set the team!
			player.Team = team
			player.Neutral = false
		else
			-- Tell the player that team could not be found :(
			player.Team = nil
			player.Neutral = true
		end
	end
end

local function onPlayerAdded(player)
	player.Chatted:connect(function (...)
		onPlayerChatted(player, ...)
	end)
end

-- Listen for players being added
for _, player in pairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

Playing/Spectating Teams

This code sample, although lengthy, is quite simple: detect when a player chats /play, then put them on the “Playing” team. When they die, move them back to the “Spectating” team.

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

-- Two teams: one for spectators (default) and one for those playing the game
local teamPlaying = Teams.Playing
local teamSpectators = Teams.Spectating
-- Command to play the game
local playCommand = "/play"

local function play(player)
	-- Set the player's team using one of the following:
	player.Team = teamPlaying
	player.TeamColor = teamPlaying.TeamColor
	-- Respawn the player (moves them to spawn location)
	player:LoadCharacter()
end

local function onPlayerDied(player, character)
	-- When someone dies, put them on the spectator team
	player.Team = teamSpectators
end

local function onPlayerSpawned(player, character)
	local human = character:WaitForChild("Humanoid")
	human.Died:connect(function () onPlayerDied(player, character) end)
end

local function onPlayerChatted(player, message)
	if message:sub(1, playCommand:len()):lower() == playCommand then
		play(player)
	end
end

local function onPlayerAdded(player)
	-- Listen for this player spawning
	if player.Character then
		onPlayerSpawned(player, player.Character)
	end
	player.CharacterAdded:connect(function ()
		onPlayerSpawned(player, player.Character)
	end)
end

-- Listen for players being added
for _, player in pairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end
Players.PlayerAdded.Connect(onPlayerAdded)