So I am working on a library extension module, and one of the functions happens to be HasProperty which is specific to roblox instances.
HasProperty = function(instance, property) -- Currently not so reliable. Tests if instance has a certain property
local successful = pcall(function()
return instance[property]
end)
return successful and not instance:FindFirstChild(property) -- Fails if instance DOES have a child named a property, will fix soon
end
It would work fine if given instance didn’t have a child with the same name as a property. Don’t know why someone would do that but I want to account for it as it’s common.
If you’re experiencing a confliction between instance properties and member objects, this would be more appropriately suited for Scripting Support as you would be trying to resolve an issue. If it’s a matter of unreliability, then it belongs here.
What part of this do you find unreliable? It’s not immediately clear, or rather I fail to see how this piece of code is unreliable.
It works fine, it’s just this little caveat. So I’m asking how to improve reliability.
Just test this and you will see what I mean:
script.Name = "Name"
local new_part = Instance.new("Part")
new_part.Parent = game:GetService("Workspace")
script.Parent = new_part
local function has_property(instance, property)
local successful = pcall(function()
return instance[property]
end)
return successful and not instance:FindFirstChild(property)
end
print(has_property(new_part, "Name"))
Could you create a temporary new instance with the same ClassName as the instance you are testing and run this function on that new instance? That way there would be no children to mess up the results.
local function HasProperty(object, prop)
local success, val = pcall(function()
return object[prop]
end)
return success and val ~= object:FindFirstChild(prop)
end