A Balanced team assigning system (help please)

Hi, I would like to know how can I create a balanced-team assigning system?
So let’s say there’s 12 players and a team game mode happens, how can i make it so there is a decently fair amount of players on each team, so in this case 6 on blue team and 6 on red?

And if there’s 7 players then like 4 on a team and 3 on a team?

Just would like someone to show me a way to create a balanced assigning function, thanks

2 Likes
local players ={"Player1"; "Player2"; "Player3"; "Player4"; "Player5"} -- Replace with the actual players

for i, player in pairs(players) do
	print(player, "is on team", (i%2)+1)
end

That will loop through the players, putting the player on the other team each time (i.e. player1 on team1, player2 on team2, player3 on team1, player4 on team2 …)

Also, you of course don’t want to just print it out, but actually put the player on a team.

Adding onto @Orbular3’s code, you can also try this here if you are trying to get it random completely.

local readytogo = lobbyteam:GetPlayers()
local currentMax = #readytogo

local function AssignRandomly()
	for i = currentMax,currentMax/2,-1 do
		local randomPlayer = readytogo[math.random(1, currentMax)]
		randomPlayer.Team = redteam
	end
	for i,remain in pairs(lobbyteam:GetPlayers()) do
		remain.Team = blueteam
	end
end

AssignRandomly()

If there are some mistakes in the script, my apologies I tested it with bricks instead of players.

1 Like

Thanks, I understand how to loop through it but I’m trying to figure out how to assign players to a team in a balanced format.

Thank you, can you explain to me what this code does? does it assign players to a team in a balanced way? so if there’s 10 players then it’s 5 on blue and 5 on red, and if there’s 7 players then 4 on one team and 3 on another team?

Lets say you have 2 teams: Team1 and Team2. Based upon that, you can switch out the print statement for:

player.Team = game:GetService("Teams"):FindFirstChild("Team".. (i%2)+1)

This will actually put the player on a team.
This system also assumes that the table of players is in a random order.

It loops the lobby Team and randomly assigns half of the players to red and assigns the remaining players to blue.

if you haven’t read it yet, i think you should give a try

  • When a Player joins a game, they will be allocated to the team with Team.AutoAssignable set to true that has the fewest players

  • However as Players leave the game this can lead to unbalanced teams as players are not reallocated.

[Contain useful info about teams and simple rebalance system]

[The script tutorial might look scary, but it actually make sure that our friends are in our team]

A huge respect if you can understand most of them and then be able to modified it according to your game design.

1 Like

By balanced do you just mean even numbers or by skill also. Im pretty sure you can assign even numbers on your own but if you dont just run a check every few minute to see if the plyr count is balanced. For skill youd need a way of measuring skill based on the game ur making. FPS games use kdr (kills/deaths) and try to get the average on each team the same

You can use a function like this. It will get the teams and players in the game. Then it will assign a near-equal amount of players to each team. The reason it can’t be perfectly equal is because there might be an un-even amount of players in the game.

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

local function RandomizeTeams()
	local players = Players:GetPlayers()
	local teams = Teams:GetTeams()

	local function assignLeftoverTeams()
		for _,player in pairs(players) do
			local teamIndex = math.random(#teams) -- get random team
			local team = teams[teamIndex]

			player.Team = team

			table.remove(teams, teamIndex)
		end
	end

	if #teams >= #players then
		assignLeftoverTeams()
	else
		local splitAmount = (#players-(#players % #teams)) / #teams -- the amount to split the players by
		
		for team in pairs(teams) do -- assign the players evenly to each team
			for i=1,splitAmount do
				local playerIndex = math.random(#players)
				local player = players[playerIndex]

				player.Team = team

				table.remove(players, playerIndex)
			end
		end
		assignLeftoverTeams()
	end
end
4 Likes