Splitting players into teams

How would I go about splitting players into teams? When a team mini game is picked they need to be split. Players that will take part in the game are put into a table. So the question is:

How would I split the players that are in that table into two separate tables that act as teams (even if there is an odd number)?

5 Likes

If you use the in pairs instead of in ipairs statement in a for loop the players should be selected randomly but only once in the for loop. The script would look something like this in that case (for 2 teams):

for i,v in pairs(players) do
  if i > #players/2 then -- Works for both an even and an uneven number
    -- Group 1, is favored in case of uneven number of players
  else
    -- Group 2
  end
end
7 Likes

Wow that is so much more simple than I thought it would be. I will try it out. Thanks!

1 Like

Make sure you understand what’s happening in that script, if you do, you should be able to divide players into three, four, five, … groups yourself. Glad I could be of assistance! Be sure to mark the post as the solution to your request if it works for you.

4 Likes