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

Why can't I change the color of specific children in my model?

Asked by 9 years ago

I have some parts, each named "Flower", in my model. I put a script in the model to change the colors of the flowers every second. The flowers don't change color, though. The script's code's below. Can anyone help?

01local flowers = script.Parent:GetChildren()
02local colors = {"Bright red", "Bright yellow", "Bright blue", "Bright violet", "Bright bluish green",
03    "Bright orange", "Institutional white"
04}
05while true do
06    if flowers.Name == "Flower" then
07        flowers.BrickColor = BrickColor.new(flowers[math.random(#flowers)])
08    end
09    wait(1)
10end

2 answers

Log in to vote
2
Answered by 9 years ago

You have a few misspells and 1 error!

Fixes

01local flowers = script.Parent:GetChildren()
02local colors = {"Bright red", "Bright yellow", "Bright blue", "Bright violet", "Bright bluish green",
03    "Bright orange", "Institutional white"
04}
05while true do
06    for _, flower in pairs (flowers) do -- Loop though all of the children.
07        if flower and flower.Name == "Flower" then -- Use flower for the object, make sure it exists.
08            flower.BrickColor = BrickColor.new(colors[math.random(1, #colors)]) -- Use colors for color.
09        end
10    end
11    wait(1)
12end

I hope this helped!

Ad
Log in to vote
1
Answered by
funyun 958 Moderation Voter
9 years ago

Wrong loop. Learn for i, v in pairs(table) do. Also, on line 7, you want to look for a random COLOR, not a random flower.

01local flowers = script.Parent:GetChildren()
02local colors = {"Bright red", "Bright yellow", "Bright blue", "Bright violet", "Bright bluish green",
03    "Bright orange", "Institutional white"
04}
05for _, v in pairs(flowers) do
06    if flowers.Name == "Flower" then
07        flowers.BrickColor = BrickColor.new(colors[math.random(1, #colors)])
08    end
09    wait(1)
10end
0
Yours didn't work for some reason... but thanks for pointing out my errors. :) imperialstar 110 — 9y

Answer this question