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?

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

2 answers

Log in to vote
2
Answered by 9 years ago

You have a few misspells and 1 error!

Fixes

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

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.

local flowers = script.Parent:GetChildren()
local colors = {"Bright red", "Bright yellow", "Bright blue", "Bright violet", "Bright bluish green",
    "Bright orange", "Institutional white"
}
for _, v in pairs(flowers) do
    if flowers.Name == "Flower" then
        flowers.BrickColor = BrickColor.new(colors[math.random(1, #colors)])
    end
    wait(1)
end

0
Yours didn't work for some reason... but thanks for pointing out my errors. :) imperialstar 110 — 9y

Answer this question