This prints true
print(game.StarterGui.ScreenGui.Home.Contents.Archivable)
This prints an error
print(game.StarterGui.ScreenGui.Home.Contents.BrickColor)
This prints nil
print(game.StarterGui.ScreenGui.Home.Contents:FindFirstChild("Archivable"))
This prints nil
print(game.StarterGui.ScreenGui.Home.Contents:FindFirstChild("BrickColor"))
Using .
or []
reports an error if it doesn't exist. :FindFirstChild()
only looks for children, not properties. I just need to see if a property on any given object exists.
EDIT:
As shown in the comments, if object:IsA("GuiBase")
worked for what I was looking to do.
if object[property_name] then print(property_name.. " exists!") end
or
if pcall(function() return object[property_name] end) then print("that property exists!") end
Edit:
The function to check for a property:
function check4property(obj, prop) return ({pcall(function()if(typeof(obj[prop])=="Instance")then error()end end)})[1] end
How to use:
check4property(game.Workspace, "Size") --returns true check4property(game.Workspace, "Baseplate") --returns false
It also checks if the userdata it gets is actually a Property or a Instance.
I am using a solution with pcall, as seen below. Note hereGoesObjectToCheck is the variable name for the instance we want to check
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 local success = pcall(function() hasProperty(hereGoesObjectToCheck, "Transparency") end)) --[[this is the part checking if the transparency property/attribute]] if success then --[[your code]] end
Yes, I know it's 5 years old question.