function getTeams() local teamNum = 1 repeat wait() local spectators = {} local teams = {"Red", "Blue"} local players = game.Players:GetChildren() for index, player in pairs(players) do if (player.TeamColor == game.Teams["Spectators"].TeamColor) then if (player.Character:findFirstChild("Torso")) then table.insert(spectators, player) end end end local randomPlayer = spectators[math.random(1, #spectators)] randomPlayer.TeamColor = game.Teams[teams[teamNum]].TeamColor local spectatorNum = 0 local players = game.Players:GetChildren() for index, player in pairs(players) do if (player.TeamColor == game.Teams["Spectators"].TeamColor) then if (player.Character:findFirstChild("Torso")) then spectatorNum = spectatorNum + 1 end end end teamNum = (teamNum == 1) and 2 or 1 until (spectatorNum == 0) end
What I have here is a function
which gives a player a random team. The problem is that they aren't random. Every single person who joins is put onto red team. The point of this script being so long and complex is to make it so if someone joins halfway through a game, they don't cause everyone else to have their teams changed.