Let’s say I want to find a part that is inside of a folder called “Parts”. And this part I want to find has an int attribute called “ID” with value of 1. Is there anyway better to find this part by its attribute than creating a function like this?
function findByAttribute (instanceTable, attribute, value)
for instanceIndex, instance in pairs(instanceTable) do
if instance:GetAttribute(attribute) == value then
return instance
end
end
end
local part = findByAttribute(Parts, "ID", 1)
I mean, any roblox global function that does this for me?
No?
I mean what is the problem with your implementation. It works fine…
Just consider making your function instance based instead of a given table (like giving the function the ancestor instance instead of its children as a table). Also, use ipairs instead of pairs.
Example:FindFirstChild(Example:GetAttribute("Ex"))
for _,i in pairs(workspace:GetDescendants()) do
if i:GetAttribute("Attribute") then
print(i.." Contains the Attribute")
end
end
If you are focused around one Object, use a ObjectValue
Yeah, It works fine, but requiring the module that this method is in, in every script I use this function, is a bit annoying. But okay, I just wanted to clear up this doubt, thank you
Sorry, I do not want a code that checks if an object has an attribute, I want to find an object that has a choosed attribute value in a table. And I already wrote a function that does this, my question was if there is any other way to do so.
I did not know that there is a module that adds attributes related functions! Thank you for that, but I think I will stick with this module I made… Thanks anyways!