# When I do #Table it keep saying 0, although it has many contents

I have a table, lets call Table. I have ~16 other tables in it. When i do

for i,v in pairs(Table) do
print(v)
end

it’ll print ~16 items.
When i do #Table, it says 0. I have no idea whats wrong. Heres a pic
Here is the output of when i print the contents and at the bottom you can see it says 0 which is the print(#Table)

1 Like

I think you meant to do `print(Table[i])`.

So you don’t have to use a pairs loop

``````for i = 1, #Table do
print(Table[i])
end``````
1 Like

The reason this is happening is, keys would not count when using the `#` operator. For that reason I assume you have these 16 tables set in a dictionarry. Can you send the code or the table’s layout please? Here is a post that explains things really well

2 Likes

Do you mind sending a pic of what you put in the table?

1 Like

So I understand now that someone told me it only works for integer indices. So i’ll tell you what i’m tryna do. The table is set via a lot of random stuff so I cant exactly show it so i’ll give an examplek

Table = {
[“Item1”] = {[“blah”] = 1},
[“Item2”] = {[“blah”] = 2}
}

What I want is to select a random table from within the dictionary thing. So I am doing Table[math.random(1,#Table)] and it is saying that there its empty.

First of all, keys are not presented as integers as you said, so even if you knew how many tables there are, `Table[math.random(1,#Table)]` isn’t valid, beacuse it’s looking for a random integer, and here we have keys.

I guess here is what you can do!

Use this function to know how many keys there are

``````function len(tab)
local sum = 0
for _, v in pairs(tab) do
sum = sum + 1
end
return sum
end
``````

And since all the tables are named `"ItemN"` where `N` is the number of the table, you can do this

``````print(Table["Item"..tostring(math.random(1, len(Table)))])
``````

Or you can do simply what @TOP_Crundee123 said, but you will need the `len` function that I gave you to find out how many keys there are

Try:

``````tonumber(Table)
``````

Im unsure why #Table isn’t working for you though.

But didn’t he say he was getting this whilst printing it normally?

You put a dictionary inside a dictionary inside a table!

Try this:

``````Table = {
[1] = {[“blah”] = 1},
[2] = {[“blah”] = 2}
}``````

What you can do is obtain the needed info from the original table and put them in an array, then use `math.random` on that array:

``````local newArray = {}
for index,value in pairs(Table) do
table.insert(newArray, value["blah"])
end

print(newArray[math.random(1, #newArray)])
``````

Or you can re-arrange the table differently to make it an array:

``````Table = {
{"Item1"; ["blah"] = 1};
{"Item2"; ["blah"] = 2};
}
``````
1 Like

Yeah I am just gonna have to do that i suppose

1 Like

The length operator does not account for string indices in tables. If you wanted to, you can use metatables:

``````local mt = {
__len = function(tbl)
local count = 0
for index, value in pairs(tbl) do
count = count + 1
end
return count
end
}

local Table = {
["Item1"] = {1};
["Item2"] = {2};
["Item3"] = {3};
}

setmetatable(Table, mt)

print(#Table)
``````
1 Like

Just a little mistake: __len doesn’t respect tables in the version of lua that’s roblox uses

1 Like