Team balancing not working correctly

Ello Everyone! So my friend and co-developer added this script for team balancing (FPS game).

For some reason, its a 50/50 that you get on the correct team.

The code is here, I have tried to debug this wayy to many times.

local function onPlayerAdded(player)
	print("Player added "..player.Name)
	local PlayerJustJoined=true
	--player:LoadCharacter()
	local function onCharacterAdded(character)
		print(character.Name .. " has spawned")
		if PlayerJustJoined==true then
			PlayerJustJoined=false
			if GameHasStarted.Value==true then
				local SelectedMap=game.ServerStorage.GameData.ServerData.MapData.SelectedMap
				print("SelectedMap.Value= "..SelectedMap.Value)
				local Map_Clone=workspace:FindFirstChild(SelectedMap.Value)
				print("Map_Clone Name.."..Map_Clone.Name)
				local MapSpawns=Map_Clone.Spawns
				BlueSpawns=MapSpawns.Blue:GetChildren()
				RedSpawns=MapSpawns.Red:GetChildren()
				
				--for i,v in pairs(PlayerTeams:GetChildren()) do
				--	local TablePlayer=v[1]
				--	local TableTeam=v[2]
				--	if TableTeam==BlueTeam then
				--		BlueTeamSize_FromTable=BlueTeamSize_FromTable+1	
				--	elseif TableTeam==RedTeam then
				--		RedTeamSize_FromTable=RedTeamSize_FromTable+1		
				--	end
				--end
				--print("BlueTeamSize "..BlueTeamSize)
				--print("RedTeamSize "..RedTeamSize)
				print("BlueTeamSize_FromTable "..BlueTeamSize_FromTable)
				print("RedTeamSize_FromTable "..RedTeamSize_FromTable )
				if BlueTeamSize_FromTable==RedTeamSize_FromTable then
					local TeamPickedRandomSeed=math.random(1,100)
					if TeamPickedRandomSeed<=50 then
						--BlueTeamSize=BlueTeamSize+1
						player.Team=BlueTeam
						--table.insert(PlayerTeams,{player.Name,BlueTeam})
						SpawnPlayer(player,BlueSpawns)
					else
						--RedTeamSize=RedTeamSize+1
						player.Team=RedTeam
						--table.insert(PlayerTeams,{player.Name,RedTeam})
						SpawnPlayer(player,RedSpawns)
					end	
				elseif BlueTeamSize_FromTable<RedTeamSize_FromTable  then--if BlueTeamSize<RedTeamSize  then
					print("Was less Blue team")
					--BlueTeamSize=BlueTeamSize+1
					BlueTeamSize_FromTable=BlueTeamSize_FromTable+1	
					player.Team=BlueTeam
					SpawnPlayer(player,BlueSpawns)
					--table.insert(PlayerTeams,{player.Name,BlueTeam})
				elseif RedTeamSize_FromTable<BlueTeamSize_FromTable then --elseif RedTeamSize<BlueTeamSize then
					print("Was less Red team")
					--RedTeamSize=RedTeamSize+1
					RedTeamSize_FromTable=RedTeamSize_FromTable+1	
					player.Team=RedTeam
					--table.insert(PlayerTeams,{player.Name,RedTeam})
					SpawnPlayer(player,RedSpawns)
				end
				
				insertTeamMarker(player)
				Events.HideGui:FireAllClients(player)
				--Players.PlayerRemoving:Connect(function(LeavingPlayer)
				--	print(LeavingPlayer.Name + " left the game!")
				--	if LeavingPlayer==player then
				
				--	end
				--end)	
				DocumentTeamMember(player)
			end
		elseif GameHasStarted.Value==true then
			
			--local PlayerGui=player.PlayerGui
			--local RespawningGui=script.RespawningGui:Clone()
			
			
			--local RespawningEvent=game.ReplicatedStorage.Events.Status.Respawning
			--delay(1,function()
			--	RespawningEvent:FireClient(player)
			--end)
			--local RespawningGui=game.ServerStorage.RespawningGui:Clone()
			--print(RespawningGui.Name)
			--RespawningGui.Parent=player.PlayerGui
			--print(RespawningGui.Parent)
			----RespawningGui.Status.RespawnGuiTimer.Disabled=false
			--print("Added respawning GUI")
			--print(player.Name)
			
			local SelectedMap=game.ServerStorage.GameData.ServerData.MapData.SelectedMap
			print("SelectedMap.Value= "..SelectedMap.Value)
			
			--local Map_Clone=workspace:WaitForChild(SelectedMap.Value,5)
			local Map_Clone=workspace:FindFirstChild(SelectedMap.Value)
			--:FindFirstChild(SelectedMap.Value)
			if Map_Clone and GameHasStarted.Value==true then
			print("Map_Clone Name.."..Map_Clone.Name)
			local MapSpawns=Map_Clone.Spawns
			BlueSpawns=MapSpawns.Blue:GetChildren()
			RedSpawns=MapSpawns.Red:GetChildren()
			delay(2,function()
				if GameHasStarted.Value==true then
				if player.Team==BlueTeam then
					SpawnPlayer(player,BlueSpawns)	
				elseif player.Team==RedTeam then
					SpawnPlayer(player,RedSpawns)	
				end
				end
				end)
			end	
		end
		--local PlayerGui=player.PlayerGui
		--local RespawningGui=script.RespawningGui:Clone()
		--RespawningGui.Parent=PlayerGui
		--print("Player RespawnIn Map01")
		--if player.Team==BlueTeam then
		--	SpawnPlayer(player,BlueSpawns)	
		--elseif player.Team==RedTeam then
		--	SpawnPlayer(player,RedSpawns)	
		--end
		
		--delay(2,function()
		--	print("Player RespawnIn Map02")	
		--	if player.Team==BlueTeam then
		--		SpawnPlayer(player,BlueSpawns)	
		--	elseif player.Team==RedTeam then
		--		SpawnPlayer(player,RedSpawns)	
		--	end
		--end)
		--character.Humanoid.Died:Connect(function()
		--	character:Destroy()
		--	player:LoadCharacter()
		--end)
		
	end
	player.CharacterAdded:Connect(onCharacterAdded)
	
	
	
	
end

Players.PlayerAdded:Connect(onPlayerAdded)

We are aiming at a release tomorrow, so any help would be nice (Yes his code is spaghetti code, he just doesn’t have posting access to get help)

1 Like

Can you clarify the issue? Is it that players don’t get assigned to the right team, and one team has more players than the other? If that is the case, the solution is to go through every player and assign every other player to the same team. An example:

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local Red = Teams.Red
local Blue = Teams.Blue

local Last = true

for _, Player in pairs(Players:GetPlayers()) do
    if Last then
        Player.Team = Blue
    else
        Player.Team = Red
    end
    Last = not Last
end

This code is for assigning the players when the game first starts. If you are asking for something else, let me know :wink:

I’ll try that, sorry for my late response! It was the issue that when a player joins, the player in question has a 50/50 chance of getting on the already larger team

Here is an example solution to that problem:

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local Red = Teams.Red
local Blue = Teams.Blue

Players.PlayerAdded:Connect(function(Player)
    local RAmount = 0
    local BAmount = 0
    for _, Other in pairs(Players:GetPlayers()) do
        if Other.Team == Red then
            RAmount += 1
        elseif Other.Team == Blue then -- Didn't use "else", as there may be Neutral players
            BAmount += 1
        end
    end
    if RAmount > BAmount then
        Player.Team = Blue
    else -- This will cause the player to be assigned to the red team, when both teams are equal
        Player.Team = Red
    end
end)