#Table returning 0

I’m trying to print how many values are in an table. I’m using #table but I get a return instance of 0. When I print the table it exists.

When printing #current it prints 0. Below I do not effect anything in the table and below that the function is called every 5 seconds.

Any help is amazing!

2 Likes

A dictionary in lua is considered to have a length of 0 as it has no order. You would need to convert your table to an array, with ascending, numerical keys. Otherwise you would need to manually count the entries by looping through the dictionary.

local current = {
    [1] = {
        ...
    },
    [2] = {
        ...
    },
    ...
}

It looks like you are trying to iterate through your table, so instead you can keep the table as is and do

for key, value in pairs(current) do
    print(key, value)
end

Though I would advise against using spelled out numerical keys when simply using an array would be preferable.

Thank you for the quick response! This clean solution is just what I needed. Greatly appreciated!

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