Team Sorting/Balancing help

Hello! im currently making a team system using roblox default teams and im confused on how i can balance teams without having the same people on a team each round. I also want to know if theres a way to favor friends joining other friends teams.

TLDR: What i want is a system that will evenly sort players into teams, but also not have players be on the same team every time.

Heres my current code:

			print("Normal Round Starting!")
			local count = 1
			----------------------
			--
			local plrs = game.Players:GetPlayers()
			local plrAmount = #plrs
			local teams = {
				game.Teams.Red,
				game.Teams.Blue
			}
			print(plrAmount)
			for i, plr in pairs(plrs) do
				plr.Neutral = true
			end
			for i = 1, plrAmount, 1 do
				plrs[i].Team = teams[count]
				count += 1
				if count > #teams then
					count = 1
				end
			end

for the most part this script works, its just not randomized.

Heres the actual “player sorting” part of the code

for i, plr in pairs(plrs) do
	plr.Neutral = true
end

for i = 1, plrAmount, 1 do				
   plrs[i].Team = teams[count]
   count += 1
   if count > #teams then
	  count = 1
   end
end

Thank you for listenning! :grinning:

1 Like

Just use pairs here because it’s random.

The easiest way with your code to randomize the teams would be to shuffle the player list. This won’t prioritize friends so you’d have to either do a post effect where you switch team members after or find another solution for that. But for the shuffling so this

local function shuffleList(list)
    for i=1, #list do
        local r = math.random(i,#list)
        local temp = list[i]
        list[i] = list[r]
        list[r] = temp
    end
    return list
end

And assuming I didn’t make a mistake with the function (didn’t run it). You can then just replace your local plrs line with this

local plrs = shuffleList(game.Players:GetPlayers())

And the rest of your code should do the rest.

1 Like

And if you want to make friends have a higher chance of being in the same team, a simple way of doing so would be to take your teams list that’s been randomized. Loop through one of the teams. If the player has a friend on the other team but not their own team. Give them a 50%(or whatever%) chance to switch spots with a random player in the other team (exclude their friend if you want. Or don’t it will still mostly work)

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