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

How do you pick a random value from a table?

Asked by 8 years ago

Trying to get used to tables, and this won't work.

color = {"Bright red","Bright green","Bright blue"}
rand = math.random(1,#color)

for i=1,3 do
    script.Parent.BrickColor = BrickColor.new(color[rand])
    wait(1)
end

The brick starts out as black and changes to one of the colors and stops. No error in output, and I've tested multiple times. It should be changing colors 3 times, and it can't be changing to the same color 3 times in a row because I've tested it over 10 times and it always stays one color.

1
You're on the right track, within those brackets "[]" you would want to put math.random(1,#color) 1 is the index value of the first object in color, and #color is the amount of objects in color. math.random will then in this case chose a number between 1 and 3. M39a9am3R 3210 — 8y
0
I did what you said and it worked, so I'm guessing you can't use variables inside the "[]"? ghostblocks 74 — 8y
0
You can use variables *anywhere* you want. Perci1 4988 — 8y
0
Why is it that when I use the variable "rand" inside the brackets it doesn't change colors 3 times but when I use math.random(1,#color) inside the brackets it works? ghostblocks 74 — 8y
0
You are already setting rand to a number before you do the loop so in that loop rand is already set to that number, to make rand a different number each time you would declare rand = math.random(1,#color) inside your for loop :3 Ryzox 220 — 8y

2 answers

Log in to vote
0
Answered by
Ryzox 220 Moderation Voter
8 years ago
color = {"Bright red","Bright green","Bright blue"}

for i=1,3 do
    rand = math.random(1,#color)
    script.Parent.BrickColor = BrickColor.new(color[rand])
    wait(1)
end

You would have to declare the rand variable in the loop as you declare it as a random number outside the loop meaning once that loop starts it's using that same random number it picked at the start and not changing it every time it loops.

0
Thanks, this is the answer I was looking for. ghostblocks 74 — 8y
Ad
Log in to vote
-1
Answered by
sigve10 94
8 years ago
-- You wont need the table with this script, but if you want it, I'm sure this script is off-topic
--color = {"Bright red","Bright green","Bright blue"}
rand = math.random (1,3)

scriptname = Script --Insert script name here for error message (not important)

if rand = 1 then
    script.Parent.BrickColor = BrickColor.new ("Bright red")
else
    if rand = 2 then
        script.Parent.BrickColor = BrickColor.new ("Bright green")
    else
        if rand = 3 then
            script.Parent.BrickColor = BrickColor.new ("Bright blue")
        else
            print ("Error: Random number was not found. This script is now prepared to be deleted!")
            print (scriptname.."is containing this error. Please go there as soon as possible")
        end
    end
end

0
tf is this? Ryzox 220 — 8y
0
The script, but it doesnt use a table. Would you like me to explain? sigve10 94 — 8y

Answer this question