Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you randomize a for i,v in pairs loop?

Asked by 5 years ago

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.

local module = {}

local TS = game:GetService("Teams")

function module.Teams(MC)
    local GT,YT = Instance.new("Team",TS),Instance.new("Team",TS) 
    GT.TeamColor,YT.TeamColor = BrickColor.new("Lime green"),BrickColor.new("New Yeller")
    GT.Name,YT.Name = "Green","Yellow"
    GT.AutoAssignable,YT.AutoAssignable = false,false
    for i,v in pairs(game.Players:GetPlayers()) do
        if i % 2 == 0 then
            v.TeamColor = BrickColor.new("Lime green")
        else
            v.TeamColor = BrickColor.new("New Yeller")
        end
        wait()
    end
end

return module

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?

1 answer

Log in to vote
1
Answered by
Vmena 87
5 years ago
Edited 5 years ago

You would have to send all the values into a table and then randomize that table.

Randomizing an array can be quite tricky but the fisher-yates shuffle algorithm is efficient and should work just fine. https://en.wikipedia.org/wiki/Fisher–Yates_shuffle

Here's the code:

function shuffle(array)
    local ShuffledArray = { }
    local random = math.random

    for index = 1, #array do
        local offset = index - 1
        local value = array[index]
        local randomIndex = offset*random()
        local flooredIndex = randomIndex - randomIndex%1

        if flooredIndex == offset then
            output[#output + 1] = value
        else
            output[#output + 1] = output[flooredIndex + 1]
            output[flooredIndex + 1] = value
        end
    end

    return ShuffledArray
end

local TS = game:GetService("Teams")

function module.Teams(MC)
    local GT,YT = Instance.new("Team",TS),Instance.new("Team",TS) 
    GT.TeamColor,YT.TeamColor = BrickColor.new("Lime green"),BrickColor.new("New Yeller")
    GT.Name,YT.Name = "Green","Yellow"
    GT.AutoAssignable,YT.AutoAssignable = false,false
    for i,v in pairs(shuffle(game.Players:GetChildren())) do
        if i % 2 == 0 then
            v.TeamColor = BrickColor.new("Lime green")
        else
            v.TeamColor = BrickColor.new("New Yeller")
        end
        wait()
    end
end

Glad I could help!

Ad

Answer this question