How to check if an Instance has Certain Properties?

Perhaps a way to do this is, using pcall. You can do some hacky trickery, by calling a function that’s supposed to just do object.Transparency, if Transparency didn’t exist, pcall will return false (because the function errored), if it existed it returns true (because it functioned correctly).

local function hasProperty(object, prop)
    local t = object[prop] --this is just done to check if the property existed, if it did nothing would happen, if it didn't an error will pop, the object[prop] is a different way of writing object.prop, (object.Transparency or object["Transparency"])
end

for i,v in pairs(script.Parent:GetDescendants()) do
   local success = pcall(function() hasProperty(v, "Transparency") end)) --this is the part checking if the transparency existed, make sure you write the property's name correctly

   if success then
    --the rest of your code
   end
end

All though I do think that this is quite costly, and you can simply reduce your original code by this, since Unions, Mesh Parts, Parts… All have one common class which is the "BasePart" class. (the decal class gotta say though)

if v:IsA("BasePart") or v:IsA("Decal") then
 -- Do Something --
end
53 Likes