Hello developers!
Sometimes we need to know if an object has a specific property, I discovered a way to do this and decided to share it with you!
(I know others discovered this trick way before me!)
I can’t really explain the code, but here is it:
function hasProperty(object, propertyName)
local success, _ = pcall(function()
object[propertyName] = object[propertyName]
end)
return success
end
--<< Example: >>--
print(hasProperty(workspace.Part, "Text")) -- prints false
print(hasProperty(someThing.TextLabel, "Text")) -- prints true
Yep, that’s all.
I hope I showed something new, bye!
I have a question, would the following would be affected or not?
Let’s say I use this function in a for loop, and check with Decals and BaseParts. They both have Transparency, but it’s not the same when you select both part and decal in properties tab. What would happen?
There is a reason why :FindFirstChild() has word “child” in its name - :FindFirstChild() is for getting object’s children, it doesn’t work with properties.
Also i’d like to make a comment on this resource in general, you don’t really need this very much, an easier way would be to not loop through a varying table, identify members through a more conventional way, etc. Generally relying on pcalls for anything like this is pretty redundant.