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

Help with If Statements?

Asked by 9 years ago

This script works perfectly fine if I remove the

and v.BrickColor == "Bright red" 

but if I don't it won't print out v and yes I have a part colored Bright red.Why won't it print out v?

I got no errors

Script:

for i,v in pairs (game.Workspace:GetChildren()) do
if v:IsA("Part") and v.BrickColor == "Bright red" 
    then 
    print(v)
 end 
end

1 answer

Log in to vote
3
Answered by 9 years ago

The primary problem is that the BrickColor is not a string.

You try and compare BrickColor with the string 'Bright red'.

There are multiple solutions to this issue -

• you can call tostring() on the BrickColor property, resulting in a string.

for i,v in pairs (game.Workspace:GetChildren()) do
if v:IsA("Part") and tostring(v.BrickColor) == "Bright red" 
    then 
    print(v)
 end 
end

• you can access the BrickColor datatype when comparing (recommended)

for i,v in pairs (game.Workspace:GetChildren()) do
if v:IsA("Part") and v.BrickColor == BrickColor.new("Bright red")
    then 
    print(v)
 end 
end

You can read more about this method here.

Ad

Answer this question