How to check if dictionary key has a function as its value?

In my module script, I set this one key to have a function as its value, but when I use an if statement to find out if the key is set to a function, the script doesn’t see the key as a function. What’s even more odd is that, when I print the dictionary together, the output says the key is equal to exactly what I put in the if statement, but when I print the key alone, it gives me something very different. Any way to properly detect that the value is a function?

if filter ~= nil then
       for i,v in ipairs(filter) do
		print(v) --["replacement"] = "function"
		if v.filter(val) == true then
			print(v.replacement) --function: 0x22bb8b8a468a5c9a
			if v.replacement == "function" then
				mathplace = v.replacement(val)
			elseif v.replacement ~= "function" then
				mathplace = v.replacement
			end
		end
	end
end

You need to check the type of the value, not the actual value.
i.e.

if typeof(v.replacement) == "function" then

Also, you can replace your elseif with just an else statement.

2 Likes

Wow, never heard of the typeof function, at least frequently. And I searched so long just for this! Thanks for the solution lol.

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