How i check if a value is a table inside of a module script?

I wrote this code inside of a module script and im trying to check if the value spriteSheet is a table.

local module = {
	objects = {}
}

function module.CreateObject(spriteSheet)
	if not typeof(spriteSheet) == "table" then error("argument must be a table") return end
	
	table.insert(module.objects, spriteSheet)
	print(module.objects)
	
	return module.objects
end

return module

Is the issue that it doesn’t work… or?

If that is the issue, one glaring cause that I can see is that you’re doing not typeof(spriteSheet) == table, this is because of operator precedence. == has higher precedence, meaning that your expression is basically being evaluated as if (not typeof(spriteSheet)) == "table", meaning that your basically doing if false == "table".

Rewrite it as if typeof(spriteSheet) ~= "table" ...
or if not (typeof(spriteSheet) == "table") if you want, though it’s easier to make mistakes going that route.

2 Likes

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