Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I check if a property exists? [Solved]

Asked by
GShocked 150
8 years ago
Edited 8 years ago

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.

1
There's no way to do this via a script, and no reason to. Just look on the wiki. Perci1 4988 — 8y
1
Well, I suppose you could use pcall, but there's still no reason to bother. Perci1 4988 — 8y
1
pcall is the only thing I can think of. GoldenPhysics 474 — 8y
0
I am using a recursive loop to check if all the objects and their children have a certain property, and if they do, change it. GShocked 150 — 8y
View all comments (4 more)
1
Instead of checking for the property, just check what object it is. Then change the property if it's the correct object. Perci1 4988 — 8y
0
Ya I just figured I'd see if this is something that exists in Rbx.Lua before resorting to that. I'll just have to check if each object is any variation of a GUI. GShocked 150 — 8y
2
You can do object:IsA("GuiBase"). It will return true if it's any GUI object. Perci1 4988 — 8y
0
OK thanks, that worked! GShocked 150 — 8y

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 5 years ago
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.

0
Does not work all of the time. For example, if a weld's Attachment 0 is nil, it will return nil, however Attachment0 is still a property. User#19006 0 — 5y
Ad
Log in to vote
0
Answered by
ErtyPL 129
3 years ago

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.

Answer this question