How would I go about getting all children who have a specific attribute set to a specific value? For example, I created a boolean attribute, and only want to reference the children that have that value set to true. Any ideas on how to do this, preferably with GetChildren() and GetAttribute()?
1 Like
Like this?
function GetChildrenWithAttribute(Object, AttributeName, AttributeValue)
assert(typeof(Object) == "Instance", "Argument 1 is not an Instance!") -- Optional thing, this is just to check if the Object is an Instance and to Error if it isnt
local tab = {} -- Table so we can store our Instances
for index,item in Object:GetChildren() do -- Iterates through provided object
if item:GetAttribute(AttributeName) == AttributeValue then -- if there is a Attribute with a specific value
table.insert(tab, item) -- Adds Instance to table
end
end
return tab -- returns the Table with Instances
end
local Array = GetChildrenWithAttribute(example, "Active", true)
1 Like
Yeah thats basically the way I have I was just looking if there was a simpler way to do it with just getchildren and getattribute and no loops
1 Like
You could, but that would more so overcomplicate things, and would be more complicate just to check every single value, if would be a lot easier to check using a loop, looking through all the values inside a table.
1 Like
You might consider using CollectionService instead of Attributes, though you might need to do some minor restructuring if you make that change. Collections are designed in a way that you can loop through everything in a collection using something like GetTagged
Instead of object:SetAttribute(attr_name, value)
or whatever it is, you’d use CollectionService::AddTag(object, tag_name)
3 Likes