Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

8 comments

1

There's no way to do this via a script, and no reason to. Just look on the wiki.

1

Well, I suppose you could use pcall, but there's still no reason to bother.

1

pcall is the only thing I can think of.

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.

1

Instead of checking for the property, just check what object it is. Then change the property if it's the correct object.

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.

2

You can do object:IsA("GuiBase"). It will return true if it's any GUI object.

0

OK thanks, that worked!

2 answers

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.

1 comment

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.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.