Making teams balanced

Hi, I was wondering if I could have suggestions to make a script for:
2 players max on home team if there is a person on team “GKH”
2 players max on away team if there is a person on team “GKA”

Current script (which just puts the player in a random team)

for _, Player in pairs(Players:GetChildren())do
		
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			if Player.Team == Team.Choosing then
				local randomTeam = Teams[math.random(1, #Teams)]
			    Player.Team = randomTeam
			
			end
		end
	end

I was also wondering if there was a way to stop the randomTeam being “GKH” or “GKA”

Thanks!

You can add int values for each team. When the player joins the team, value increases, when he leaves, it decreases. And every time assigning the team you check the values and assign accordingly

You dont need to instantiate an instance to track team players if you can

local teamNumber = #YourTeam:GetPlayers()

You can do a lots of things. Easiest is adding int value for each team in explorer (4 teams, 4 values). That way team counts are accessible everywhere, and im sure 4 values wont affect performance. If anything they might even improve performance, since operations with them will be much simpler than with various tables, bindable events (to pass value through scripts) and etc.

Consider looking at the Developer Hub article, or these threads:

I already read through some threads, but none of those is what I am looking for.

1 Like

Thanks, I will try that. ‎‎‎‎
‎‎‎

1 Like

Something like this?

for _, Player in pairs(Players:GetChildren())do
		
		
			if Player.Team == Team.Choosing then
				local randomTeam = Teams[math.random(1, #Teams)]
			  
				if HomeVal < 3 then
					Player.Team = Home
					HomeVal += 1
				elseif AwayVal < 3 then
					Player.Team = Away
					AwayVal += 1
			
			end
		end
	end

But I don’t think that will make the teams balanced if there are not 6 players in one server.

You could try checking which one has the lower players, and adding the player to that team. If this is not the intended behavior, could you please describe the desired one in a little more detail?

1 Like

like @Bilon said, you d want to assign the team that has lower players, in this case you need random only when the teams are equal

for _, Player in pairs(Players:GetChildren())do
		
		
			if Player.Team == Team.Choosing then
				
			  
				if HomeVal < AwayVal then
					Player.Team = Home
					HomeVal += 1
				elseif HomeVal >  AwayVal then
					Player.Team = Away
					AwayVal += 1
                                elseif HomeVal == AwayVal then
                                        local randomTeam = Teams[math.random(1, #Teams)]
                                        Player.Team = randomTeam
			
			end
		end
	end
if HomeVal < AwayVal then
					Player.Team = Home
					HomeVal += 1
				elseif AwayVal < HomeVal then
					Player.Team = Away
					AwayVal += 1

yeah but randomteam would put you in these teams, which i dont want
image

randomVal = math.random(1,2)
if randomVal == 1 then
   Player.Team = home
elseif randomVal == 2 then
   Player.Team = away
end

Of course you need to modify this code, this is just to give you an idea

This is what you should add, if the teams have the same amount of players the unassigned player is just not gonna be placed in any of them

Oh i didnt see that he wants to have no more than a certain amount of players in team

1 Like

Thank you for the idea, but there is one issue when i tested.
it doesnt put any player on a team

Can you show your script? And the explorer with values

The values are both set to 0.
image
And the script:

for _, Player in pairs(Players:GetChildren())do

		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
		if Player.Team == Team.Choosing then


			if HomeVal < AwayVal then
				Player.Team = Home
				HomeVal += 1
			elseif HomeVal >  AwayVal then
				Player.Team = Away
				AwayVal += 1
			elseif HomeVal == AwayVal  then
				
				local randomTeam = math.random(1, 2)
				if randomTeam == 1 then
					Player.Team = 1
				elseif randomTeam == 2 then
					Player.Team = 2
				end 
	         end
		  end
	   end
	end

change this to

local randomTeam = math.random(1, 2)
				if randomTeam == 1 then
					Player.Team = Home
				elseif randomTeam == 2 then
					Player.Team = Away
				end 

omg i didn’t notice that lol, idk why i put one
having a fresh pair of eyes really works, ill try it!

1 Like