Search code examples
luainstanceroblox

How do you find all the objects/instances in a folder/model with a specific attribute in lua/roblox lua


I am working on a game in Roblox and need a little help finding objects with attribute values. It is a tycoon for anyone here familiar with Roblox. I would like to know how you can find all instances with say the attribute Unlock_ID: 1, and then when you press the corresponding button, it will unlock it, or make it show up on the screen. I know how to make the objects unlock, I just don't know how to get all the objects with that attribute.

To put it more simply:

I want to find all the objects/instances inside a section with a specific attribute and a specific value and put them inside a table.

I hope this makes sense.


Solution

  • You can use a for i,v in ipairs loops along with the Folder:GetChildren() as your iteration directory. Then you can insert the instance (using table.insert(table,element) into a table if their part:GetAttribute('Unlock_ID') is equal to 1. To achieve this, simply do the following:

    local partsWithAttribute = {}
    local folder = workspace.Folder -- change this to whatever path you want to iterate through
    
    for i,part in ipairs(folder:GetChildren()) do
        if part:GetAttribute('Unlock_ID') == 1 then
            table.insert(partsWithAttribute,part)
        end
    end