โ† Back to all scripts

Team System

Assign players to teams on join with auto-balancing.

SystemsIntermediateServerScriptService
team-system.lua
-- Team System with Auto-Balance
local Teams = game:GetService("Teams")

local function getOrCreateTeam(name, color)
    local t = Teams:FindFirstChild(name)
    if t then return t end
    local team = Instance.new("Team", Teams)
    team.Name = name; team.TeamColor = BrickColor.new(color)
    team.AutoAssignable = false; return team
end

local red  = getOrCreateTeam("Red Team",  "Bright red")
local blue = getOrCreateTeam("Blue Team", "Bright blue")

local function assignTeam(player)
    player.Team = #red:GetPlayers() <= #blue:GetPlayers() and red or blue
    player.TeamColor = player.Team.TeamColor
end

game.Players.PlayerAdded:Connect(assignTeam)
game.Players.PlayerRemoving:Connect(function()
    task.wait(1)
    for _, p in ipairs(game.Players:GetPlayers()) do assignTeam(p) end
end)
Related scripts in Systems
Basic Round SystemIntermediate โ†’Checkpoint SystemBeginner โ†’Day/Night CycleBeginner โ†’
Something broken or not working?
๐Ÿ”ง Try the AI Script Fixer โ†’