#table only works for consecutively assigned numerical indexes, so t = {"a", "b", "c"} or t={[1] = "a", [2] = "b", [3] = "c"}
As you're using itemIds as the index, you're ending up with nonconsecutive elements. Use pairs(t) to iterate through and count elements
Code:
local count = 0
for k in pairs(t) do count = count + 1 end