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

How do I get length of a table/array without using # or table.getn() ?

I want to get length of an array/table.

local mytable = {one = {value = 1}, two = {value = 2}}

print(#mytable)
print(table.getn(mytable))

Both will return 0. I want to find the way that will print 2 in this situation.

1 answer

✓ Accepted answer

You can't use the length operator on dictionaries, you'd have to loop through it.

local mytable = {one = {value = 1}, two = {value = 2}}

local function countNumberOfIndexes(thisTable)
	local count = 0
	for i,v in pairs(thisTable) do
		count = count + 1
	end
	return count
end

print( countNumberOfIndexes(mytable) )

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