My random team picker is unbalanced

i want a random team picker but I wanted it to be balanced. so if there are like 4 people in the server, i don’t want a 3v1, or 4v0.

local players = game.Players:GetChildren()

for i = 1, players do
local Teams = game:GetService(“Teams”):GetChildren()
local chosenTeam = Teams[math.random(1, teams)]
players[i].Team = chosenTeam
end

Have you read Team Balancing?

2 Likes

THANK YOU I read it all and it helped ALOT. I didn’t the friends part but everything else was SUPER helpful!

local Game = game
local Players = Game:GetService("Players")
local Teams = Game:GetService("Teams")

local RedTeam = Teams["Red Team"]
local BlueTeam = Teams["Blue Team"]
local RandomObject = Random.new()

local function OnPlayerAdded(Player)
	local RedPlayers, BluePlayers = RedTeam:GetPlayers(), BlueTeam:GetPlayers()
	Player.Team = if #RedPlayers > #BluePlayers then BlueTeam elseif #BluePlayers > #RedPlayers then RedTeam else if (os.time % 2) == 0 then RedTeam else BlueTeam
end

local function OnPlayerRemoving(Player)
	local RedPlayers, BluePlayers = RedTeam:GetPlayers(), BlueTeam:GetPlayers()
	if (#RedPlayers - #BluePlayers) > 1 then
		RedPlayers[RandomObject:NextInteger(1, #RedPlayers)].Team = BlueTeam
	elseif (#BluePlayers - #RedPlayers) > 1 then
		BluePlayers[RandomObject:NextInteger(1, #BluePlayers)].Team = RedTeam
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)