The TeamColor property determines which team a Player is associated with according to that Team’s
Team/TeamColor. Changing this property will change
Player/Team according to whichever team has the same
DataType/BrickColor for their
Team/TeamColor. If no Team object has the associated TeamColor, the player will not be associated with a team.
It’s often a better idea to set
Player/Team to the respective
Team instead of using this property. Setting this property often leads to repetition of the same BrickColor value for a certain teams across many scripts; this is something you want to avoid when adhering to the don’t-repeat-yourself principle.
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)