Shuffle the array of players randomly, then split it down the middle.
You may also want to look at the first example at the bottom of this page, which seems to keep teams balanced as players join/leave. Not exactly you’re question but maybe useful.
function FYShuffle( tInput )
local tReturn = {}
for i = #tInput, 1, -1 do
local j = math.random(i)
tInput[i], tInput[j] = tInput[j], tInput[i]
table.insert(tReturn, tInput[i])
end
return tReturn
end
So your algorithm just becomes
local players = game.Players:GetPlayers()
local shuffled = FYShuffle(players)
Then put the players in the first half of shuffled on one team and the rest on the other team.