function getTeams()
local teamNumber = 1
repeat wait()
local lobby = {}
local teams = {"Red", "Blue"}
for _, player in pairs(game.Players:GetChildren()) 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 spectatorNumber = 0
for _, player in pairs(game.Players:GetChildren()) do
if player.TeamColor == game.Teams["Lobby"].TeamColor then
local character = player.Character
if character and character:FindFirstChild("Torso") then
spectatorNumber = spectatorNumber + 1
end
end
end
--print(teamNumber)
--print(spectatorNumber)
teamNumber = (teamNumber == 1) and 2 or 1
until spectatorNumber == 0
end
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:wait()
getTeams()
end)
For some reason every player joins red team. Can anyone see why? I don't want the code altered much. The code has to be in a function, as I call it later in the script.

6 comments
wait() is deprecated, use Wait(). http://wiki.roblox.com/index.php?title=RBXScriptSignal#wait_2
What do you mean?
this might sound dumb but have you tried using math.randomseed(tick())?
I think that would just completely random, I need the teams to be kept even. Completely random could still result in even teams.
but you could add checks to make sure teams are even
use game.Players:GetPlayers()) btw