How do I sort players into two team

I am making a game with teams, but I don’t know how to take half the players in the game and put them on one team and the other half on the other team, I would like to know how to do this.

If you don’t have a round system, and you just want them to be on two seperate teams as they log in. You can check the “Auto-Assignable” box in the teams properties.

You can change a player’s team by doing

game.Players.(player).Team = game.Teams.(TeamName)

Here’s how I would do it.

local Teams = {game.Teams.Red, game.Teams.Blue}

local SplitTeams = function()
    local Players = game.Players:GetPlayers()
    for i = 1, #Players do
        Players[i].Team = Teams[i%#Teams]
    end
end

SplitTeams()

Please take the time to understand what’s going on. If you have any questions let me know.

I do have a round system that I want to put the team assining into and this is the script, but not the entire script, because it is very long, but if I need to put in the entire script I can.

The round system

	RS.Events.RoundStartingEvent.OnServerEvent:Connect(function()
		mapVoting = false
		local votes = mapVotingParts.VoteMap1.Votes
		local votes2 = mapVotingParts.VoteMap2.Votes
		if votes.Value > votes2.Value then
			mapVotingParts.ChosenMap.Value = votes.Parent.SurfaceGui.MapName.Text
		elseif votes2.Value > votes.Value then
			mapVotingParts.ChosenMap.Value = votes2.Parent.SurfaceGui.MapName.Text
		elseif votes.Value == votes2.Value then
			local randomMap = math.random(1,2)
			if randomMap == 1 then
				mapVotingParts.ChosenMap.Value = votes.Parent.SurfaceGui.MapName.Text
			else
				mapVotingParts.ChosenMap.Value = votes2.Parent.SurfaceGui.MapName.Text
			end
		end
		wait(0.1)
		if mapVotingParts.ChosenMap.Value == "CASTLE" then
			if inRound == false then
				RS.Maps.CastleMap:Clone().Parent = workspace
				
				local greenTeam = Instance.new("Team")
				greenTeam.TeamColor = BrickColor.new("Lime green")
				greenTeam.Name = "Green Team"
				greenTeam.Parent = game:GetService("Teams")
				
				local tealTeam = Instance.new("Team")
				tealTeam.TeamColor = BrickColor.new("Toothpaste")
				tealTeam.Name = "Teal Team"
				tealTeam.Parent = game:GetService("Teams")
				
				-- I want to put it in here
				
				RS.Events.KillAllPlayersEvent:FireAllClients()
				RS.Events.KillAllPlayersEvent.OnServerEvent:Connect(function(player, char)
					char.Humanoid.Health = 0
				end)
			end
		else
			RS.Maps.DesertMap:Clone().Parent = workspace
		end
		inRound = true
		votes.Value = 0
		votes2.Value = 0
		votes.Parent.SurfaceGui.MapName.Text = ""
		votes.Parent.SurfaceGui.VotesGui.Text = ""
		votes2.Parent.SurfaceGui.MapName.Text = ""
		votes2.Parent.SurfaceGui.VotesGui.Text = ""
	end)

You could just insert the function which @polill00 provided somewhere in the script, and then call it.
Ex:

local tealTeam = Instance.new("Team")
tealTeam.TeamColor = BrickColor.new("Toothpaste")
tealTeam.Name = "Teal Team"
tealTeam.Parent = game:GetService("Teams")
			
SplitTeams() --Call the function
			
RS.Events.KillAllPlayersEvent:FireAllClients()
RS.Events.KillAllPlayersEvent.OnServerEvent:Connect(function(player, char)
	char.Humanoid.Health = 0
end)
1 Like