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

Using # in a table counts other tables inside?

When I put a # in front of my table name, it errors because of the code in the else of the if statement. My table consists of something like this:

local erroringTable = {["s1"]={["id"]="",["desc"]="Description"}}

and when I put a hashtag in front of it,

print(#erroringTable)

is way less than it should be! It's 0! If you the answer, please reply. Thanks!

1 comment

1

You can't count the length of a hashtable.

1 answer

To count hash table (dictionary), you need to iterate through all keys:

local function Count(table)
	local count = 0
	for _, _ in pairs(table) do
		count = count + 1
	end
	return count
end

local erroringTable = {["s1"]={["id"]="",["desc"]="Description"}}

print(Count(erroringTable))

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