For some reason, whenever the intermission ends, the players get teamed. In this instance, I need players to be auto balanced on 2 seperate teams, for example bright green and bright blue. Whenever I enter the game with 6 people, 4 go on 1 team and 2 on the other? Please help? Sometimes this works and sometimes doesn't.

##Code:

local function TeamCount(Team)
	local Count = 0
	
	for i, v in pairs(game.Players:GetPlayers()) do
		if v.TeamColor == Team.Value then
			Count = Count +1
		end
	end
	
	return Count
end

local function CountPlayers()
	local C = 0
	
	for i, v in pairs(workspace.Players:GetChildren()) do
		C = C +1
	end
	
	return C
end

local function Count_Team(Team)
	local Count = 0
	
	for x, y in pairs(Team:GetPlayers()) do
		Count = Count +1
	end
	
	return Count
end

local function AssignTeam(Player)
	local Count = 0
	
	for i, v in pairs(game.Teams:GetChildren()) do
		if v.TeamColor ~= BrickColor.new("Medium stone grey") and v.TeamColor ~= BrickColor.new("Really blue")  and v.TeamColor ~= BrickColor.new("Camo") and Count_Team(v) <= 3 then
			Player.TeamColor = v.TeamColor
		end
	end
	
	return
end

do
	wait()
	
	for i, v in pairs(game.Players:GetPlayers()) do
		local Value = Instance.new("BoolValue", workspace.Players)
		Value.Name = v.Name
	end
	
	local Players = workspace.Players:GetChildren()
	
	if CountPlayers() == 3 then
		local P_1 = math.random(1, #Players)
		local Pick_1 = Players[P_1]
		
		local Player = game.Players(Pick_1.Name)
		Pick_1:Destroy()
		
		Player.TeamColor = BrickColor.new("Bright blue")
		
		local Players = workspace.Players:GetChildren()
		
		local P_2 = math.random(1, #Players)
		local Pick_2 = Players[P_2]
		
		local Player_2 = game.Players(Pick_2.Name)
		Pick_2:Destroy()
		
		Player_2.TeamColor = BrickColor.new("Bright green")
		
		local Players = workspace.Players:GetChildren()

		local P_3 = math.random(1, #Players)
		local Pick_3 = Players[P_3]

		local Player_3 = game.Players(Pick_3.Name)
		Pick_3:Destroy()

		Player_3.TeamColor = BrickColor.new("Bright blue")
		
		
		elseif CountPlayers() < 3 then
			local P_1 = math.random(1, #Players)
			local Pick_1 = Players[P_1]

			local Player = game.Players(Pick_1.Name)
			Pick_1:Destroy()

			Player.TeamColor = BrickColor.new("Bright blue")

			local Players = workspace.Players:GetChildren()

			local P_2 = math.random(1, #Players)
			local Pick_2 = Players[P_2]

			local Player_2 = game.Players(Pick_2.Name)
			Pick_2:Destroy()

			Player_2.TeamColor = BrickColor.new("Bright green")

			local Players = workspace.Players:GetChildren()

			local P_3 = math.random(1, #Players)
			local Pick_3 = Players[P_3]

			local Player_3 = game.Players(Pick_3.Name)
			Pick_3:Destroy()

			Player_3.TeamColor = BrickColor.new("Bright blue")



		for i, y in pairs(workspace.Players:GetChildren()) do
			local v = game.Players:FindFirstChild(y.Name)
			
			local Teams = workspace.Game_Teams:GetChildren()
			local Random_Team = math.random(1, #Teams)
			
			local Team = Teams[Random_Team]
			
			if TeamCount(Team) >= 3 then
				AssignTeam(v)
			else
			    v.TeamColor = Team.Value
			end
		end
		
	else
		
		for i, v in pairs(game.Players:GetPlayers()) do
			local Teams = workspace.Game_Teams:GetChildren()
			local Random_Team = math.random(1, #Teams)
			
			local Team = Teams[Random_Team]
			
			if TeamCount(Team) >= 3 then
				AssignTeam(v)
			else
				v.TeamColor = Team.Value
			end
		end
		
	end
	
	workspace.Teams_Remaining:ClearAllChildren()
	
	for i, v in pairs(script.Parent.Teams:GetChildren()) do
		v:Clone().Parent = workspace.Teams_Remaining
	end

	workspace.Game_Info.Status.Value = "GAME IN PROGRESS"
	game.ReplicatedStorage.StartTimer:Fire()
	workspace.Common_Scripts.Teleport_Players.Disabled = false
	
	workspace.Players:ClearAllChildren()
	
	wait(.2)
	
	script.Disabled = true
end

1 comment

0

Make sure you encode your code properly when using a code block. Maintain your code between these lines ~~~~~~~~~~~Your Code~~~~~~