Roblox Tutorial - Auto Team Assign Command Source Code

In this tutorial, I’ll be teaching you how to make your very own automatic team assign command; this command is super useful and can make managing your Roblox training so much easier!

Source Code:

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


local GroupId = 7
local MinRank = 0
local prefix = "/"

local MaxTeamSize = 2

local TeamsTable = {}

local Colors = {BrickColor.new("Baby blue"), BrickColor.new("Pastel green"), BrickColor.new("Pastel orange"), BrickColor.new("Pastel yellow"), BrickColor.new("Pastel blue-green"), BrickColor.new("Pastel light blue"), BrickColor.new("Pastel violet")}

local function CreateTeams()
	local TeamsGenerated = 0
	local AmountOfPlayers = #Players:GetPlayers()
	local TeamsRequired = math.ceil(AmountOfPlayers / MaxTeamSize + 0.5) 
	print(TeamsRequired)
	for count = 1,TeamsRequired do
		TeamsGenerated += 1
		local TeamCreated = Instance.new("Team")
		TeamCreated.Name = "Team " ..TeamsGenerated
		TeamCreated.TeamColor = Colors[math.random(1, #Colors)]
		TeamCreated.AutoAssignable = false
		TeamCreated.Parent = Teams
		table.insert(TeamsTable, 0)
	end
end


local function AssignTeams()
	local CurrentTeamToAddTo = 1
	for i,v in ipairs(Players:GetChildren()) do
		local TeamFound = game:GetService("Teams")["Team " ..CurrentTeamToAddTo]
		if #TeamFound:GetPlayers() >= MaxTeamSize then 
			CurrentTeamToAddTo = CurrentTeamToAddTo +1
			print(CurrentTeamToAddTo)
			local NextTeam = game:GetService("Teams")["Team " ..CurrentTeamToAddTo]
			print(NextTeam.Name)
			print(v.Name)
			v.Team = NextTeam
		else
			print(v.Name)
			v.Team = TeamFound
		end
	end
	local FinalCheck = game:GetService("Teams")["Team " ..CurrentTeamToAddTo +1]
	print()
	if #FinalCheck:GetPlayers() <= 0  then
		FinalCheck:Destroy()
	end
end


game.Players.PlayerAdded:Connect(function(Player)
	if Player:GetRankInGroup(GroupId) >= MinRank then
		Player.Chatted:Connect(function(msg)
			if msg == prefix.. "assign" then
				CreateTeams()
				AssignTeams()
			end
		end)
	end
end)

For any scripting support questions head to: #scripting-support

This topic was automatically closed 1 minute after the last reply. New replies are no longer allowed.