Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 7 years ago
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().

1
Maybe this results to BrickColor.new(BrickColor.Random()) ? RubenKan 3615 — 7y
0
No, the same error still occurs iGlaciem 72 — 7y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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
0
Thanks, I figured it out the same time as you! iGlaciem 72 — 7y
Ad

Answer this question