Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why they can't recognize lengh of my dictionary?

local Foods = {
	Salt = {Taste = "Salty", Hunger = "1"},
	Cookie = {Taste = "Sweet", Hunger = "100"}
}

print(#Foods) --- Output =  0

I dont know why this happen. Why they cant recognize the length?

1 answer

✓ Accepted answer

Dictionaries don't really exist in Lua. There's only tables.

Anyway, there are no values in your tables. You only assigned values within your table. If you do want to check how many keys and all are in your table, you must use a for loop and count how many entries there are.


local Foods = {
  Salt = 1,
  Cookie = 2
}
 
print(#Foods) --- Output =  0

local entriesNumber = 0
for i, _ in pairs(Foods) do
  entriesNumber = entriesNumber + 1
end

print(entriesNumber) -- Output = 2

1 comment

0

Btw, the i variable is not needed either. You can just replace it with an underscore, if you care so much about that.

This discussion is preserved as it was. New answers, comments, and votes are closed.