Hello all. I thought it would be worthwhile to share some code my friend Casualist once wrote on the Scripters forum for randomizing teams (thread).
-- The below function can be used for any array (non-zero) of players and teams. function shuffleTeams(players, teams) for i = 1, #players do table.remove(players,math.random(#players)).TeamColor = teams[i%#teams+1].TeamColor end end
It uses the neat functionality of
table.remove returning the element that was removed from the table.
The distribution of players across teams will be equal within an error of one player. That's because the team is not randomly selected, the player that is removed from the table is.