Coding help regarding team sorting

Hi, I hope the following code makes sense. I have set up a round based game where the first player is set as the main character (team x) then removed from table.
I then created a for loop to distribute the remaining players in the remaining two teams. However, when starting the game, the players successfully switch from the lobby to the game, and the chosen player is successfuly set as team x, however the program only seems to execute the final ‘else’ statement when testing the game with 6 overall players- meaning 1 player goes to team x and the rest to team z.
Any clue why this is? (I suspect it is an error with my syntax)
Thanks
local Players = game.Players:GetPlayers()
local players = teams.Lobby:GetPlayers()

``````-- Sort first role

local index = math.random(1,#players)
players[index].Team = teams["x"]
table.remove(players,index)

-- Sort Remaining Players

for i, player in ipairs (players) do
if players == 5 then
player1 = math.random(1,#players)
player[player1].Team = teams["y"]
table.remove(players, player1)
player2 = math.random(1,#players)
player[player2].Team = teams["y"]
table.remove(players, player2)
player3 = math.random(1,#players)
player[player3].Team = teams["y"]
table.remove(players, player3)
player.Team = teams["z"] -- rest of the players set to this team
elseif players == 6 then
player1 = math.random(1,#players)
player[player1].Team = teams["y"]
table.remove(players, player1)
player2 = math.random(1,#players)
player[player2].Team = teams["y"]
table.remove(players, player2)
player3 = math.random(1,#players)
player[player3].Team = teams["y"]
table.remove(players, player3)
player4 = math.random(1,#players)
player[player4].Team = teams["y"]
table.remove(players, player4)
player.Team = teams["z"] -- rest of the players set to this team
else	-- Just put this in to complete the statement flow
player.Team = teams["z"]
end
end
``````

The reason why it’s using the last else statement is because players is not equal to 5 or 6. The code is seeing players as a table and not a number so that’s why. What you want to do instead is

if table.getn(players) == number then

or

if #players == number then

From what I can see in your code, you want to team a random player into `x`, team the remaining players - 2 in `y` and the rest into `z`, can’t you do this instead?

``````local Players = game.Players:GetPlayers()
local players = teams.Lobby:GetPlayers()
local teams = game:GetService("Teams")

-- Sort first role

local index = math.random(#players)
players[index].Team = teams["x"]
table.remove(players,index)

-- Sort Remaining Players

local total = #players

for i = 1, (total - 2) do
randIndex = math.random(#players)
players[randIndex].Team = teams["y"]
table.remove(players, randIndex)
end

for i, player in ipairs(players) do
player.Team = teams["z"]
end
``````

And yes @B2ontwitch is correct about that because you’re checking if a table is equal to a number, you forgot to do the length operation

Im assume you were doing that for y because the total was 5, you teamed 3, when it was 6, you teamed 4

Hi, when implementing these changes, the lobby → game function seems to break, as only the first player is assigned to team x- and the rest stay in the lobby. Any clue why?

This works, thank you very much!

1 Like

Anytime! If you have more questions or issues don’t be afraid to make another post!

Hi, just quickly i forgot to ask, this works for a set number of players. The main idea i had is for the lobby to be able to host a maximum of 10 players, and the game would start after a while at a minimum of 6 players. How would i go about improving this code to cater for additional situations (as when the players get to say 10, i would like more than just 2 players on team z aka i would like to alter the split e.g. 1x 5y 3z)

Are oyu planning on adding a maximum amount of players in all teams? So like, there can be a maximum of 5 people in the Y team, you can’t have 6 or more

ps i am currently attempting a code I will let you know if it is successful.

I have managed to make it work for a different number of players! thank you for your help!
-Additionally, do you have any idea if you can send players to multiple c frames when loading the game as opposed to one? as it seems funny when the players all sit on top of eachothers heads when waiting to get into the map. This is the current code:

function teleportPlayers()
local players = game.Players:GetPlayers()
for i,v in pairs(players) do
v.Character.HumanoidRootPart.CFrame = currentmap:FindFirstChild(chosenmap.Value).Palace.Spawn.CFrame
end
end

You can put multiple spawns in your map and have it randomly choose one of those spawns and set the RootPart’s CFrame to the randomly chosen spawn

Put the Spawns in a folder

1 Like