Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I execute a script to split players into evenly matched teams?

The teams are blue and red i want them to be split into even teams at a certain point during the game (not at the beggining)I have tried everything I know to do this one script was successful but it did not evenly split the teams can someone please help.

1 answer

✓ Accepted answer

I understand "even teams" as teams with the same amount of members. If there is an uneven amount, one team will have one more member than the other team.

function Rebalance()
	local teams = {game.Teams.Red, game.Teams.Blue}
	if math.random(1,2) == 1 then
		teams = {game.Teams.Blue, game.Teams.Red} -- Otherwise always the red team has 1+ member
	end
	local plist = game.Players:GetPlayers()
	local function random_player()
		local new = math.random(1, #plist)
		local player = plist[new]
		table.remove(plist, new)
		return player
	end
	for i = 1, #plist do 
		local new player = random_player
		new_player.TeamColor = teams[(i % 2) + 1]
	end
end

Just call this function at any given moment.

Please note that players which join at the game spawn will not be added to any of those teams - you need to put them in manually. You haven't given any behavior for this so I cannot define that for you... :)

This discussion is preserved as it was. New answers, comments, and votes are closed.