This prints true
1 | print (game.StarterGui.ScreenGui.Home.Contents.Archivable) |
This prints an error
1 | print (game.StarterGui.ScreenGui.Home.Contents.BrickColor) |
This prints nil
1 | print (game.StarterGui.ScreenGui.Home.Contents:FindFirstChild( "Archivable" )) |
This prints nil
1 | 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.
1 | if object [ property_name ] then |
2 | print (property_name.. " exists!" ) |
3 | end |
or
1 | if pcall ( function () return object [ property_name ] end ) then |
2 | print ( "that property exists!" ) |
3 | end |
Edit:
The function to check for a property:
1 | function check 4 property(obj, prop) |
2 | return ( { pcall ( function () if (typeof(obj [ prop ] ) = = "Instance" ) then error () end end ) } ) [ 1 ] |
3 | end |
How to use:
1 | check 4 property(game.Workspace, "Size" ) |
2 | --returns true |
3 | check 4 property(game.Workspace, "Baseplate" ) |
4 | --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
01 | local function hasProperty(object, prop) |
02 | 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"])]] |
03 | end |
04 |
05 |
06 | local success = pcall ( function () hasProperty(hereGoesObjectToCheck, "Transparency" ) end )) --[[this is the part checking if the transparency property/attribute]] |
07 |
08 | if success then |
09 | --[[your code]] |
10 | end |
Yes, I know it's 5 years old question.