Balanced team select

Hello!
I was wondering for my game how I would make a balanced team select UI? Please help!

You just have to make a script that checks the quantity of players in each time by using :GetPlayers(). Once the script knows the quantity of players in each team (considering there are only 2 teams), do the following operation:

local PlayerInTeam1 = Team1:GetPlayers()
local PlayerInTeam2 = Team2:GetPlayers()

local BalanceResult = #PlayerInTeam1 - #PlayersInTeam2

if BalanceResult >= 0 then
   -- run the rest of the code
end

Basically what you’re doing is comparing the number of players in team one and the players in team two, if the result is a negative number, that means that one of the teams (In that case, it would be Team1) is not balanced. Replace Team1 by the team the player wants to join, and replace Team2 by the opposite team.

1 Like

I want to make a team select like arsenal where you choose between 4 teams and it locks the team if one team has one more player then the other team. (btw I know another way to do this but it will take about 500 lines)

Then just compare all the 4 teams player count.

function CheckCount()
     local Players = game.Players
     local Team1 = 0
     local Team2 = 0
     local Team3 = 0
     local Team4 = 0
     for i = 1,#Players do
          if Players[i].Team = -- TeamHere1 then
               local Team1 = Team1 + 1
          end
          if Players[i].Team = -- TeamHere2 then
               local Team2 = Team2 + 1
          end
          -- Repeat for each team.
    end
      if Team1 < Team2 and Team1 < Team3 and Team1 < Team4 then
          --- Team2 Has least players.
      else
           if Team2 < Team1 and Team2 < Team3 and Team2 < Team4 then
               --- Team1 Has least players.
           else
                   --- Repeat for each team.
            end
           -- If nothing still then they can join any team
      end
end

And theres not an easier way to do that?

there probably is but i prefer to use scripts im comfortable with. Its still easy, this only took me like 2 minutes.

I have a modified version of your script and it does not work.

The and statements don’t seem to work.

Did you correctly count the number of players?
Are the modifications you have done correct?
Did you complete the script where I put ‘-- Repeat for each team’?

I’m using values in the script so I modified it.

I already looked at It btw. Team balancing auto assigns teams.

Yeah but there is some code you can use for what you want to achieve.

Yes there is an easier solution and more flexible because it doesn’t matter how many teams you have

local teams = {} -- put all teams into that table
local minvalue = math.huge
local minindex = -1

for i, team in ipairs(teams) do
    local count = #team:GetPlayers()
    if count < minvalue then
        minvalue = count
        minindex = i
    end
end

if minindex ~= -1 then
    print("team with least players is " .. teams[minindex].Name)
else
    -- handle case when there are no teams in the `teams` table
end

You could set a minimum value by checking the number of every team’s players, and lock the teams that have more players than that minimum.
Example)

local teamService = game:GetService("Teams")

local teams = teamService:GetTeams()
local smallestTeams = {}

local minValue = math.huge

for _, team in pairs(teams) do
	
	if #team:GetPlayers() < minValue then
		
		minValue = #team:GetPlayers()
		smallestTeams = {}
		table.insert(smallestTeams, team)
		
	elseif #team:GetPlayers() == minValue then
		
		table.insert(smallestTeams, team)
		
	end
	
end

for _, team in pairs(teams) do
	
	if not table.find(smallestTeams, team) then
		
		-- Lock the team
		
	end
	
end

On both of the scripts I don’t get the minValue = math.Huge

tested your script and it is only compatible with one team in a table.

That wouldn’t make sense if I wrote a script only for one team xd It evalueates the minimum over all teams in the game.

The whole script breaks if two teams have the same amount of players.