How do I make :GetAttributes() return an array instead of a dictionary?

Hello. How do I make :GetAttributes() return an array instead of a dictionary? The reason why I want this is because table.find doesn’t work with dictionaries.

This is what :GetAttributes() returns:
image

What I want it to return:
image

Im guessing there is no way to get it to return an array, but how do I turn a dictionary into an array?

2 Likes

You could probably make a table then loop through the dictionary of attributes and insert the value into the table you created like this.

local attributes = {}

for i,v in YourInstance:GetAttributes() do
    table.insert(attributes,v)
end

Why?

Its intended to be a dictionary for a reason, otherwise you wont know what value is which when using them.

If you’re focused on a few set values, create a new table, iterate through said dictionary, and put said values into the new table to be used as an array. But at that point, you’re pretty much better off making your own attribute system, it would be much more managable as it wouldn’t be worth your time to create seperate attributes for an array.

Have you checked this out?

2 Likes

I know how to search through dictionaries, but I dont want it to be a dictionary because i dont use the key. As u can see here:
image

The key is the same as the value, which is why i’d prefer an array:
image

I might just use values instead of attributes and do :GetChildren()

You can just do what @Blue_muf suggested: loop through the dictionary and insert the values to a seperate table, then just return that table

2 Likes

Yeah, im gonna do that. Thanks

Why don’t you just loop through the dictionary though? There is no performance gain from removing the keys. All tables in lua are associative arrays.
Doing this:

for _, value in dictionary do

end

Is the exact same as:

for _, value in list do

end

If you want to get the index you can pass the table to the ipairs() function.

I’m gonna use values instead because you cant get the number of Attributes like this: (because its a dictionary) #Instance:GetAttributes()

You would have to do:

local NumOfAttributes
for _, v in Instance:GetAttributes() do
     NumOfAttributes += 1
end

Whereas with values you can just do #Instance:GetChildren()

I appreciate your help though

Fyi you can also overwrite the length operator to achieve the same result using only the dictionary but at that point it really depends on personal preference. Use whatever implementation you think suits you best. This solution has a time complexity of O(n) where n is the number of hashed values. If you need the length of the dictionary multiple times converting it into a sequence using the marked solution makes more sense as lua fetches the length of sequences with a time complexity of O(1) as far as I’m aware.

local t = {["hello"] = "world" }

local lengthmetatable = {}
lengthmetatable.__len = function(t)
    local elements = 0
    for _, val in pairs(t) do
        elements +=  1
    end
    return elements
end

setmetatable(t, lengthmetatable)

print(#t) --Prints 1

Thats cool but its easier to do #Value:GetChildren() ngl

Thanks anyways

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