How can you detect if a part has a certain property?

Hello Developers!

So while I was exploring loops in scripting, I came across an issue I could not resolve. I was trying to make a GUI that made all parts in workspace can-collide off. This did not work. The script was something like this: (This was on a serverScript)

for i,v in pairs(workspace:GeChildren()) do
   if v.CanCollide then
       v.CanCollide = false
    end
end

I got an error. It was detecting a folder that I had put into workspace and obviously, a folder doesn’t have the canCollide property. I was trying to see if the property existed in the part but I think the script thought that I wanted to see if it was true or false. I still am not sure how I could see if an instance has a property so If you guys have any tips on how to, please share.

Thanks in advance

1 Like

Or you can do the :IsA function, that can work as well if you just want certain Instances

3 Likes

I’m not aware if there’s a way of detecting if an instance has a specific property.
However, you can just check if the instance is a BasePart.

for i,v in pairs(workspace:GetChildren()) do
   if v:IsA("BasePart") and v.CanCollide then
      v.CanCollide = false
   end
end
2 Likes

use the IsA() function, for example

for i,v in pairs(workspace:GetChildren()) do
   if v:IsA("BasePart")  then
       v.CanCollide = false
    end
end
3 Likes

The lazy way (Ask the script to brute force and ignore errors):

pcall(function()
     v.CanCollide = false
end)

The non-lazy way (Identify what instance type have the property you want to change) :

if v:IsA("BasePart") then
     v.CanCollide = false
end
1 Like

So pcall is lazy? And is basepart every type of part?

Yes, basepart is every part type
pcall is not necessarily lazy, it just avoids errors. I find it useful when altering Transparency in a character since both Decal and BasePart have Transparency
(also it can hide stack trace if your pcall errors (even though you can print the error message))

Ok thanks. But mesh part also a basepart right?

Yeah i’m pretty sure
might be wrong thou since i’ve never worked with mesh parts (yet)

1 Like