Why wont this work?

Here is the code:

local Colors = {“Red”, “Blue”, “Yellow”, “Green”, “Black”, “Brown”, “Orange”}
local ColorNames = {

"Really Red",
"Dark Blue",
"New Yeller",
"Lime Green",
"Really Black",
"CGA Brown",
"Deep Orange"

}

print(ColorNames[1])

for i, part in pairs(game.Workspace:GetChildren()) do
local RanCol = math.random(1, 6)
if part.Name == “ColorPart” then
print(ColorNames[RanCol])
part.BrickColor = BrickColor.new(ColorNames[RanCol])

end

end

Funnily enough, I think BrickColor takes a string which is caps sensitive.

This means “Really Red” won’t work and you have to use “Really red” instead, aka only first word is capped.

Except “Pastel Blue”.

Another thing: You have 7 colors in your table but you’re doing math.random(1, 6). Try choosing between the number of colors by setting the maximum to the number of indexes that exist in your table:

math.random(1, #ColorNames)

Ok thank you! I will get back to you in a sec

It works, however some of the parts dont change color and they stay grey

local ColorNames = {
	"Really red",
	"Dark blue",
	"New yeller",
	"Lime green",
	"Really black",
	"CGA brown",
	"Deep orange"
}

print(ColorNames[1])

for _, part in workspace:GetChildren() do
	if part:IsA("BasePart") and part.Name == "ColorPart" then
		local RanCol = math.random(1, #ColorNames)
		part.BrickColor = BrickColor.new(ColorNames[RanCol])
	end
end

Some still stay grey… I honestly dont know why

Can I see the Workspace hierarchy? Like, its children.

The Name’s are mispelled.

What names? Like the ones in the table?

Yes

No? That’s solved. Their issue is that some parts are not colouring. I believe some of their parts have a different parent, and they might have to use :GetDescendants() instead.

They’re not misspelled. BrickColor takes a caps sensitive string.

Unless I’m like, drunk or something.

One moment please… Ill be right back

The problem was I spelled “New Yeller” like “New yeller”. Ig it is spelled that way

So the colors themselves work or not? I mean, is your issue now only that some parts are not colouring?

It works now, roblox seems to be very picky with spelling for brickcolor

Thank you everyone! You guys were really helpful.

2 Likes

Anyway, I would Recommend you use CollectionService, it would be much more efficient for what you are trying to do, Instead of you iterating though children or Descendants (That takes a lot of time for process)
It would be much more efficient to use CollectionService as running it in a for loop will already have access to the parts, and can easily manipulate them.

You can use Tag Editor (That is now a Built-In Plugin), or do it manually by:

CollectionService:AddTag(Part, "ColorPart")

for you to iterate through every Part with a tag, you would use GetTagged, here is an example:

for number,Part in CollectionService:GetTagged("ColorPart") do -- Gets Tagged Instances
    -- code
end

Hope this helps

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.