Iâ€™m not sure if that game is the same with this one:

https://www.roblox.com/games/1215581239/Doomspire-Brickbattle?refPageId=508198e7-3f81-4da3-9bee-04c1e379000

But this one is open sourced so you can check out how they randomize the teams.

Just click the â€śEditâ€ť button

Edit:

The game control script under server script service has this function used for randomizing the teams:

```
script:WaitForChild("ShuffleTeams").OnInvoke = function()
local teams = AssignableTeams()
local numberOfTeams = #teams
local playersList = game.Players:GetPlayers()
local numberOfPlayers = #playersList
-- Quit early if there are no teams, this avoids a divide by zero
if #teams == 0 or #playersList == 0 then
return
end
local playersPerTeam = math.floor(numberOfPlayers / numberOfTeams)
local leftOverPlayers = numberOfPlayers - (playersPerTeam * numberOfTeams)
local teamCount = {}
for _, team in ipairs(teams) do
teamCount[team] = 0
for i = 1, playersPerTeam do
local k = math.random(1, #playersList)
local player = playersList[k]
if player then
player.TeamColor = team.TeamColor
teamCount[team] = teamCount[team] + 1
table.remove(playersList, k)
end
end
end
-- Now place the remaining players
for i = 1, leftOverPlayers do
local k, player = next(playersList)
local teamIndex = math.random(1, #teams)
local team = teams[teamIndex]
if player and teamIndex then
player.TeamColor = team.TeamColor
table.remove(playersList, k)
table.remove(teams, teamIndex)
end
end
end
```

Just to explain whatâ€™s going on here

```
local teams = AssignableTeams() --table of available teams in-game
local numberOfTeams = #teams --number of teams
local playersList = game.Players:GetPlayers() --table of current players
local numberOfPlayers = #playersList --number of current players
```

```
if #teams == 0 or #playersList == 0 then --this checks if the number of teams or players is equal to 0
return --so it stops the function to save resources or end the game
end
```

```
local playersPerTeam = math.floor(numberOfPlayers / numberOfTeams) --computes for the number of players per team
local leftOverPlayers = numberOfPlayers - (playersPerTeam * numberOfTeams) --determines the number of players to be left over due to an un-even count
```

```
local teamCount = {} --this will hold the number of players per team
for _, team in ipairs(teams) do --this loops through the available teams
teamCount[team] = 0 --this creates the team slots in the teamCount table
for i = 1, playersPerTeam do --loops the number of players to be placed per team
local k = math.random(1, #playersList) --picks a random number
local player = playersList[k] --gets a random player
if player then --checks if the player exists
player.TeamColor = team.TeamColor --sets the player's team color
teamCount[team] = teamCount[team] + 1 --sets the new player count on the current team
table.remove(playersList, k) --removes the selected player from the players list to prevent re-shuffling
end
end
end
```

```
for i = 1, leftOverPlayers do --loops the number of left over players
local k, player = next(playersList) --gets the index and player object from the left over players
local teamIndex = math.random(1, #teams) -- gets a random number
local team = teams[teamIndex] --gets a random team using random number
if player and teamIndex then -- checks if the player and team exists
player.TeamColor = team.TeamColor --sets player's team color
table.remove(playersList, k) --removes player from player list to prevent re-shuffling
table.remove(teams, teamIndex) --removes team to prevent unbalanced teams
end
end
```