This is my attempt at trying to make a function that gets all the players in the game and putting them on teams and making sure that even amounts of players are on each team. The problem is tho if another player joins after the game has started they arent put on a team and cant play till the next round. However, I want the new player to still be sorted into a team, without changing the current teams, just adding them to a team, keeping them even.
function getTeams()
local teamNumber = 1
repeat wait()
local lobby = {}
local teams = {"Red", "Blue"}
for _, player in pairs(game.Players:GetPlayers()) do
if player.TeamColor == game.Teams["Lobby"].TeamColor then
local character = player.Character
if character and character:FindFirstChild("Torso") then
table.insert(lobby, player)
end
end
end
local randomPlayer = lobby[math.random(1, #lobby)]
randomPlayer.TeamColor = game.Teams[teams[teamNumber]].TeamColor
toMap(randomPlayer)
local lobbyNumber = 0
for _, player in pairs(game.Players:GetChildren()) do
if player.TeamColor == game.Teams["Lobby"].TeamColor then
if player.Character:FindFirstChild("Torso") then
lobbyNumber = lobbyNumber + 1
end
end
end
teamNumber = teamNumber == 1 and 2 or 1
until lobbyNumber == 0
end

1 comment
Always add new players in the team with the fewest players. If there is a tie, flip a coin (generate a random number) and make a decision based on that.