Dividing the players into 2 teams

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the community!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

AQika

Member
Jun 28, 2023
31
3
8
As the title says i need some code to loop all the players and divide them into 2 teams, red and blue if the number of players is odd one team should be bigger i dont rlly care
 
As the title says i need some code to loop all the players and divide them into 2 teams, red and blue if the number of players is odd one team should be bigger i dont rlly care
Add all players to a list, shuffle the list, get floor(size of all players/2) and set it to a variable, loop the list, add 1 to a variable, if the previous variable is less than floor(size of all players/2) add to team 1 else add to team 2
 
Add all players to a list, shuffle the list, get floor(size of all players/2) and set it to a variable, loop the list, add 1 to a variable, if the previous variable is less than floor(size of all players/2) add to team 1 else add to team 2

"and set it to a variable" is not the correct way to say that, the correct way to express setting a variable to a value is "and set a variable to it" or "and store it into a variable" (which is more used in other languages), because you don't set something a value to a variable, you set a variable to a value (assuming that the value is not another variable or a metadata tag).

That said, here is how I would do it:
SQL:
loop shuffled players:
  add 1 to {_count}
  if mod({_count},2) = 0:
    # red
  else:
    # blue
Note: I'm not sure if shuffled returns actual numbers as indices instead of strings, also not sure how the index replacement works, documentation says it does return numbers but I'm not able to test that right now, try using loop-index instead of {_count} and see how that goes, it might be a string, if so parse it as integer.
 
This might be what you want:
On join:
set {_teamid} to a random integer between 1 and 2
if {_teamid} is 1:
set {team::%player%} to "red"
if {_teamid} is 2:
set {team::%player%} to "blue"
wait 0.1 seconds
send "&eCongrats! You got assigned to team %{team::%player%}%"
If you prefer players to only be sorted once, you can add "first" on line 1 right after "on"
 
This might be what you want:

If you prefer players to only be sorted once, you can add "first" on line 1 right after "on"
I dont want random chances, i want half of the players into the red team and half into blue team
Code:
            loop all players:
                add loop-player to {alive::*}
                set {respawnCounter::%loop-player%} to 3
                if {teamChooser} is true:
                    add loop-player to {teamRed::*}
                    send "&cYou have been recruited to the red team." to loop-player
                    set {teamChooser} to false
                    teleport loop-player to {redSpawn}
                if {teamChooser} is false:
                    add loop-player to {teamBlue::*}
                    send "&9You have been recruited to the blue team." to loop-player
                    set {teamChooser} to true
                    teleport loop-player to {blueSpawn}

this is my code right now. it does not work.
 
I dont want random chances, i want half of the players into the red team and half into blue team
Code:
            loop all players:
                add loop-player to {alive::*}
                set {respawnCounter::%loop-player%} to 3
                if {teamChooser} is true:
                    add loop-player to {teamRed::*}
                    send "&cYou have been recruited to the red team." to loop-player
                    set {teamChooser} to false
                    teleport loop-player to {redSpawn}
                if {teamChooser} is false:
                    add loop-player to {teamBlue::*}
                    send "&9You have been recruited to the blue team." to loop-player
                    set {teamChooser} to true
                    teleport loop-player to {blueSpawn}

this is my code right now. it does not work.
Then use every other solution given to you
 
"and set it to a variable" is not the correct way to say that, the correct way to express setting a variable to a value is "and set a variable to it" or "and store it into a variable" (which is more used in other languages), because you don't set something a value to a variable, you set a variable to a value (assuming that the value is not another variable or a metadata tag).

That said, here is how I would do it:
SQL:
loop shuffled players:
  add 1 to {_count}
  if mod({_count},2) = 0:
    # red
  else:
    # blue
Note: I'm not sure if shuffled returns actual numbers as indices instead of strings, also not sure how the index replacement works, documentation says it does return numbers but I'm not able to test that right now, try using loop-index instead of {_count} and see how that goes, it might be a string, if so parse it as integer.
This is what i wanted, this worked perfectly but i wasnt home to implement it! i didnt know we can use Modulo