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

Why do I get the error Workspace.Script:21:bad argument #1 to'new' (Color3 expected,got BrickColor)?

colors = {"Really red", "Cyan", "Lime green"}

for i = 1,10 do
	colors[#colors + 1] = BrickColor.Random()
	wait()
end
print(table[10])

for i,v in pairs(colors) do
	print(v)
	game.Workspace.Baseplate.BrickColor = BrickColor.new(v)
	wait(2)
end


I'm learning how to use tables and I don't understand why I need to use Color3.new(). Even when I use Color3.new(), the script expects BrickColor.new().

2 comments

1

Maybe this results to BrickColor.new(BrickColor.Random()) ?

0

No, the same error still occurs

1 answer

✓ Accepted answer

Your Problem

When you store the 10 random BrickColors in your colors table, they are already BrickColor datatypes. So this means that line 11 is like saying BrickColor.new( [BrickColor] ) which doesn't work.

Your error

There is a constructor that allows you to input Color3 datatypes as arguments for the BrickColor.new function, which is why you recieved the error :)

How to fix

If you apply some logical operators, as well as the type function you can easily get passed this.

The type function returns the datatype of the given value. You can check if the current iteration is either one of your manually inputted strings or a randomly added value, and go about setting the BrickColor property accordingly.


workspace.Baseplate.BrickColor = (type(v) == "string" and BrickColor.new(v)) or v

1 comment

0

Thanks, I figured it out the same time as you!

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