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)]-- Error here (Line 77)
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
ServerScriptService.GameScript:77: bad argument #2 to 'random' (interval is empty)
The code here is suppose to get all the players in a game and give them all random teams and keeping it even. It also is suppose to give a team to any new player who may join late, but still keep the teams even. For some reason it is error at local randomPlayer = lobby[math.random(1, #lobby)] and idk why. It seems to work if you have say 4 players in the game and the starts, but if another play joins mid game the output has that error. How can I get it to just add the player to the game?

1 comment
try and do it maybe using playeradded event