BrickColor.new can be used to condense multiple things into a BrickColor, right? Such as Color3 and even strings. But does it often get confused?

I was running this as Part of a Loop:

v.BrickColor = BrickColor.new(Colors[counter]) 
--Colors[counter] indexes a string name for a BrickColor in an array

Many times it would work very smoothly, but every once in a while, given the same exact conditions as before, it would produce this error

bad argument #1 to 'new' (Color3 expected, got BrickColor)

Would someone please explain this?

Colors is an array defined at the very beginning of the script. It is filled in once by function CountColors, But then is lost as the script is ran again. Thus, string.gmatch is used to retrieve array items from StringVal Colors.

It is used to restore the colors of a model the second time around the script is called, using Function RestoreColor

local Colors = {}
local counter = 1
for v in string.gmatch(Tool.Colors.Value, "([%a%s.]+),") do
	Colors[counter]= v
	print(Colors[counter])
	counter = counter +1
end
function EditAllParts(Parts, Transparency, Color)
	for _, v in ipairs(Parts:GetChildren()) do
		if v:IsA("BasePart") then
			v.BrickColor = BrickColor.new(Color)
			v.Transparency = Transparency
		elseif v:IsA("Model") then
			EditAllParts(v, Transparency)
		end
	end
end
local function CountColor(Parts)
	for _, v in ipairs(Parts:GetChildren()) do
		if v:IsA("BasePart") then
			Tool.Colors.Value = Tool.Colors.Value..tostring(v.BrickColor)..","
			Colors[counter] = v.BrickColor
			counter = counter + 1
			
		elseif v:IsA("Model") then
			CountColor(v)
		end
	end
end

5 comments

0

If you are using the :GetChildren() Function, It may be a problem there. Could you post a little more of the script?

0

Can you include the code that defines Colors and counter? It's really hard to guess what's wrong with so little to go on.

0

Very Well then. Updated

0

But WHY IPAIRS

0

So it does every command, in order