Unable to obtain number of objects in a table

So, I have a table of functions, and I want to pick a random Function to use.

for some reason the # operator doesn’t work.

code:

local Attacks = {
	Close = {
		Swing = function()
			print("ok")
		end,
		Jump = function()
			print("ok")
		end,
	}
}
print(#Attacks) -- returns 0

The # operator only works on the array part of a Lua table, the part with consecutive natural number indices. There is no shortcut for counting keys in a dictionary, you’ll either have to increment/decrement your own counter, or run a pairs for loop on the table and count how many times it loops.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.