Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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!

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

1 comment

0

Yours didn't work for some reason... but thanks for pointing out my errors. :)

This discussion is preserved as it was. New answers, comments, and votes are closed.