How do you randomize a for i,v in pairs loop?
So, basically, I have this module script that, whenever called, it creates a set of teams and assigns players to them. However, it utilizes a for i,v in pairs loop to do this, which means the teams aren't really random at all. Simply because it inserts v (The player) into a table and always runs that same table every time the function is called. If my understanding is correct, that is.
03 | local TS = game:GetService( "Teams" ) |
05 | function module.Teams(MC) |
06 | local GT,YT = Instance.new( "Team" ,TS),Instance.new( "Team" ,TS) |
07 | GT.TeamColor,YT.TeamColor = BrickColor.new( "Lime green" ),BrickColor.new( "New Yeller" ) |
08 | GT.Name,YT.Name = "Green" , "Yellow" |
09 | GT.AutoAssignable,YT.AutoAssignable = false , false |
10 | for i,v in pairs (game.Players:GetPlayers()) do |
12 | v.TeamColor = BrickColor.new( "Lime green" ) |
14 | v.TeamColor = BrickColor.new( "New Yeller" ) |
Basically what this does is;
4 Players in the game
Function is called
Green Team and Yellow Team are created
Player 1 is inserted into Yellow Team
Player 2 is inserted into Green Team
Player 3 is inserted into Yellow Team
Player 4 is inserted into Green Team
Function is called again
Repeats the same process
What I'm trying to figure out is how to randomize that to where maybe the first time it's called, Player 1 and Player 2 are on the same team, Player 3 and Player 4 on the other. Then, when called again, Player 2 and Player 3 are on the same team and Player 1 and Player 4 are on the same team. I want it to always be random. How would I accomplish this?