How would I split a number of players evenly across teams?

  1. What do you want to achieve? When the administrator of the game clicks on 4 teams, for example, every single player gets split evenly amoungst 4 teams. Or, if I click 3 teams, all the players will get split evenly through the 3 teams. It’s pretty similar from there.

  2. What is the issue? Unabled to find a valid solution for the issue. Tried using my own knowledge of programming & problem solving, was unable to figure it out myself. Google is not showing many answers specific enough for this issue.

  3. What solutions have you tried so far? YouTube, Google, Friends, etc. I’m only finding team selector tutorials as in Player chooses.

I do not want the player to be able to choose teams, only the administrator that automatically assigns it to everyone else.

1 Like

This will distribute the players into however many teams you give it, I don’t know if you wanted it to create the teams but that’s what I did

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

local function splitPlayersIntoTeams(players,teamCount)
	local playerSplit = math.max(math.ceil((#players/teamCount)-.5),1)
	local teams = {}
	
	local function getNewTeam(teamIndex)
		local teamName = 'Team'..teamIndex
		local team = Teams:FindFirstChild(teamName)
		if not team then
			team = Instance.new('Team')
			team.Name = teamName
			team.TeamColor = BrickColor.Random()
			team.Parent = Teams
		end
		teams[teamIndex] = team
		return team
	end
	
	for index,player in players do
		local teamIndex = math.min(math.ceil(index/playerSplit),teamCount)
		local team = (teams[teamIndex] or getNewTeam(teamIndex)) or teams[1]
		if not team then continue end
		player.Team = team
	end
end

splitPlayersIntoTeams(Players:GetPlayers(),4)

Hopefully you can use this to figure out what you’re trying to do

the script below will only show you how to split players evenly in teams:

-- variables
game.Players.PlayerAdded:Wait()
local team_num = 4 -- the amount of teams you want you can change the value of that variable however you want
local turn = 1
local players = game:GetService("Players"):GetChildren()
teams = {} -- a table that contains the teams, it will be useful later/if you already have teams do not write this
-- setup teams (do not paste this part if you already have teams)
local team_colors = {BrickColor.new("Really red"),BrickColor.new("Lime green"),BrickColor.new("Really blue"),BrickColor.new("Deep orange")}--choose the colors of the teams,team one will have the first color
for i = 1,team_num,1 do
	print(i)
	team = Instance.new("Team")
	team.Name = "Team "..tostring(i) -- names the team created to team {number of team}
	print(team.Name)
	team.Parent = game:GetService("Teams")
	team.AutoAssignable = false --prevents player from entering the team accidentally
	team.TeamColor = team_colors[i]--adds the team color if its the first team being created,then i will be equal to one,then color will be the first index of team colors wich is red right now
	table.insert(teams,team)--insert the team into the table teams,it will be useful soon
end

--assign players to the created teams
print(players)
for i,player in pairs(players) do
	player.Team = teams[turn]-- if you didnt write the teams setup you should store the teams in a table called teams
	turn += 1
	if turn > team_num then turn = 1 end --prevents the turn from going over the amount of teams
end

now i know there is a lot of comments but i tried my best to explain the script using comments ,so after pasting the script you can delete the comments. Also make sure to put this code in an event or teams will be created when you start the game

3 Likes

Wow I made mine more complicated with the math, yours is much simpler hah, nice.

1 Like