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?
01 | local flowers = script.Parent:GetChildren() |
02 | local colors = { "Bright red" , "Bright yellow" , "Bright blue" , "Bright violet" , "Bright bluish green" , |
03 | "Bright orange" , "Institutional white" |
04 | } |
05 | while true do |
06 | if flowers.Name = = "Flower" then |
07 | flowers.BrickColor = BrickColor.new(flowers [ math.random(#flowers) ] ) |
08 | end |
09 | wait( 1 ) |
10 | end |
You have a few misspells and 1 error!
Fixes
01 | local flowers = script.Parent:GetChildren() |
02 | local colors = { "Bright red" , "Bright yellow" , "Bright blue" , "Bright violet" , "Bright bluish green" , |
03 | "Bright orange" , "Institutional white" |
04 | } |
05 | while 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 ) |
12 | end |
I hope this helped!
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.
01 | local flowers = script.Parent:GetChildren() |
02 | local colors = { "Bright red" , "Bright yellow" , "Bright blue" , "Bright violet" , "Bright bluish green" , |
03 | "Bright orange" , "Institutional white" |
04 | } |
05 | for _, 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 ) |
10 | end |