#Instance:GetAttributes() returns 0

Hello!

I’m wanting to find out the amount of attributes that they’re on an Instance and I’ve used #Instance:GetAttributes() for this but when I do it will always be 0 for some reason when there’s multiple attributes. Is this a Roblox bug or is there something I’m not doing right or? I’ve tested #Instance:GetChildren() and that worked fine but for some reason #GetAttributes() is always 0.

One way to get around this is to just do the following code, but I’d prefer to just go #GetAttributes() instead of having to create a loop that does the same thing.

local Number = 0
local Folder = game.ServerStorage.Folder:GetAttributes()

for i, v in Folder do
    Number += 1
end

print(Number)

So is this a bug or an intended thing? If its a bug then could someone please report this bug in the #bug-reports category as I don’t have access to it right now.

2 Likes

GetAttributes() returns a dictionary, and the length operator only works properly on sequential arrays.

image

I’ve seen a freature request for a function that counts elements of dictionaries and mixed tables as part of the table library. Perhaps it will get implemented in the future.

5 Likes

Does instance have any attributes?

image

2 Likes

What’s the difference between an array and a dictionary and table?

2 Likes

Yes, this test one had 4 and I’ve tested it with different amounts and nothing.

1 Like
  • Arrays and dictionaries are all tables
  • Arrays are tables only with numerical indices
-- array
local array = {1, 2, 32, 48} -- {[1]: 1, [2]: 2, [3]: 32, [4]: 48}
  • Dictionaries are tables with indices that aren’t necessarily numbers
local dictionary = {
    ["index"] = "value"
}
4 Likes

I’ve instead gone to try a table for this aswell, so does this mean the # operator won’t work on this either?

The table looks like this:

local Stations = {
       [1] = “StationOne”,
       [2] = “StationTwo”
}

Does this mean I can’t use the # operator on this table to get how many stations they’re are for a route?

1 Like

Yes, the # should work for that

2 Likes

But doesn’t the GetAttributes() return the same sort of table?

1 Like

Instance:GetAttributes() returns a dictionary

Instance:GetAttributes()
-- Looks like this:
{
  ["attribute_name"] = "attribute value"
}
1 Like

But isn’t the table I sent you also a dictionary or is that an array?

Is a dictionary where an index is a string or something and is an array where the index is a number?

1 Like

array

Yes; arrays don’t have to have numbers as their values, just the indices


Yes

Oh alright thanks? Indices means index right?

1 Like

To clear some confusion, a dictionaries look like this:

local CashValues = {
["Silver"] = 30,
["Diamond"] = 100
}

and tables look like this:

local Fruits = {"Apples", "Bananas", "Grapes"}

- or -

local Fruits = {
[1] = "Apples"
[2] = "Fruits"
}
1 Like