Need help with balancing teams

Hi.

I made this script which randomises your team but I have one problem with it. It doesn’t balance the team. I have 4 teams and I want to put 1 player on each team. (there are 4 players max in one server)

The script:

function module.randomiseTeams()
	module.changeStatus("Randomizing teams..." , true)
	
	local TeamsTable = {"Arsenal", "Barcelona", "Dortmund" , "Madrid"}
	for _, Player in pairs(Players:GetPlayers()) do
		if Player.Team == Teams.Lobby then
			local RandomTeam = TeamsTable[math.random(1 , #TeamsTable)]
			Player.Team = Teams:FindFirstChild(RandomTeam)
		end
	end
end

Any help would be appriciated, thanks!

Assuming you are talking about balancing skill level between teams. A possible approach is:

local x = #TeamsTable
--get the top x amount of players and assign them to the x teams randomly
--get the 2nd best group of x players and assign them each to a different team randomly
--etc

For your example this would mean getting the top 4 players and making sure they are all on different random teams, then get the 4th-8th best players and assign them to random teams, etc.

One way I’ve done is using modulus (%) in order to determine teams!

Little example:

local _players = game:GetService("Players")

local TeamsTable = {"Arsenal", "Barcelona", "Dortmund" , "Madrid"}
for index, player in pairs(_players:GetPlayers()) do
	print(player.Name, TeamsTable[index%#TeamsTable+1])	
end

1 Like
local teams = {
	{"Arsenal",{}};
	{"Barcelona",{}};
	{"Dortmund",{}};
	{"Madrid",{}};
}
-- {teamname,players}

function resetteams()
	for i = 1,#teams do
		local team = teams[i]
		for p = 1,#team[2] do
			team[2][p].Team = "neutral"; -- can change the name
		end
		team[2] = {}
	end
end

function setteam(p,team)
	p.Team = team[1];
	table.insert(team[2],p)
end

function getnearteam()
	local actualteam, n = nil, math.huge;
	local sum = 0;
	
	for i = 1,#teams do -- check
		sum += #teams[i][2]
	end

	if sum == 0 then
		actualteam = teams[math.random(1,#teams)]
	else
		for i = 1,#teams do
			local ct = teams[i]
			local plrs = #ct[2]
			if plrs < n then
				actualteam = ct
				n = plrs
			end
		end
	end
	return actualteam;
end

function logic(p)
	local getteam = getnearteam();
	setteam(p,getteam)
end

function setteamsforplayers()
	resetteams()
	for _,player in pairs(game.Players:GetPlayers()) do
		logic(player)
	end
end

Thanks for the script! How would I team players using this though?

Nevermind:

local TeamsTable = {"Arsenal", "Barcelona", "Dortmund" , "Madrid"}
	
	for index, Player in pairs(Players:GetPlayers()) do
		local RandomTeam = TeamsTable[index%#TeamsTable+1]
		Player.Team = Teams:FindFirstChild(RandomTeam)
		Player:LoadCharacter()
end

Thanks for your help!

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

local TeamsTable = {"Arsenal", "Barcelona", "Dortmund" , "Madrid"}
for index, player in pairs(_players:GetPlayers()) do
	player.Team = _teams[TeamsTable[index%#TeamsTable+1]]
end

You should also add the lobby logic.

Ok, will do; thanks for your help!

1 Like