Getting Instances by its Attributes

Hey!

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.


Nope, there isn’t yet.

1 Like

You could, But that just seems tedious,

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 :slight_smile:

At least you can edit it at any time for once instead of editing every script…

You can also use a pre-made module for attributes related functions like AttributeUtil or I can actually share you a module I made.

1 Like

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.

Didnt say you needed it

As if Currently no, There isn’t another way of doing it,

If anything you already have something that works, so there isn’t really a purpose in continuing this Topic.

1 Like

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!

1 Like

Sorry, I probably misunderstood your reply or something like that, I am learning English so I propably made a grammar error. Sorry.

Thank you for your help anyways :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.