What im doing wrong in table?

local TriggerStats = {
		["Name"] = 1;
		["Name2"] = 2;
	}
	print(#TriggerStats)

Its prints: 0
(even though it should be 2)

getting the table length through # only works for indexes of numbers. Since the indexes are strings, it doesn’t register and prints 0. The only way to do it with string indexes is to loop over it.

local TriggerStats = {
	["Name"] = 1;
	["Name2"] = 2;
}

function getlen(tbl)
	local l = 0
	
	for i,v in pairs(tbl) l += 1 end
	
	return l
end

print(getlen(TriggerStats))

ok. But how do i get the length?

Actually was in the middle of editing the comment when you said that.

1 Like