Split players in half to a team

How do I make a script that split all players in the server to a specific team.

You could make a for loop that loops through everyone in the game, check if the index (i) is divisible by 0, and then assign those players to team 1, and assign the odd indexes to team 2.

Would this do the trick?

--define our TeamColors
local Team1 = BrickColor.new("Red")
local Team2 = BrickColor.new("Blue")
--the function we will call when we want to split the players into these teams
function SplitIntoTeams()
local NumPlayers = 0
local TeamDivided = 0
for i, v in pairs (game.Players:GetChildren()) do --This is to count how many players are in the game and sets how many need to be in each team
NumPlayers = NumPlayers + 1
TeamDivided = NumPlayers / 2
for i, v in pairs (game.Players:GetChildren()) do --This loop goes through and actually assigns the teams to the players
if NumPlayers > TeamDivided then
v.TeamColor = Team1
v.TeamColor = Team2
NumPlayers = NumPlayers -1

Depends, if you want it to be a set team or a completely random team.

For a random team you could do this: