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

How to randomly choose a string from a table?

Asked by 8 years ago

Hey there. I've been having a bit of trouble with a simple piece of code. I was wondering if anyone here could help. Basically, I made a table, and used a portion of code to pick out a random one of the strings within said table. It's not working the way I want it though.

local colorTable = {
        "Really red";
        "Deep orange";
        "New Yeller";
        "Forest green";
        "Lapis";
        "Teal";
        "Magenta";
        "Pink";
        "Carnation pink";
        "Really black";
}
    beam.BrickColor = BrickColor.new(math.random(#colorTable))

The 'beam' part is made out of neon material. However, I've changed this material to other things and I've still encountered the same issue.

My assumption is that I have the wrong equation in use within the BrickColor.new() parameters.

Help is greatly appreciated.

-Kon

2 answers

Log in to vote
1
Answered by 8 years ago

You were pretty close.

local colorTable = {
        "Really red";
        "Deep orange";
        "New Yeller";
        "Forest green";
        "Lapis";
        "Teal";
        "Magenta";
        "Pink";
        "Carnation pink";
        "Really black";
}

local function rancolor(tab)
    return BrickColor.new(tab[math.random(1,#tab)]) --a random number between 1 and the length of the table
end

beam.BrickColor = rancolor(colorTable)
Ad
Log in to vote
0
Answered by 8 years ago

Good seeing you again Aquathorn. Thanks.

Answer this question