__newindex meta method is not firing of a table's metatable's metatable

Hey there, I have an issue where I have a meta table called Meta which is a metatable for another metatable called Meta2, and Meta2 is the meta table for a table called SimplyNotMeta, when I am doing SimplyNotMeta["Pizza] = "Italy, it is not firing the __newindex metamethod in metatable Meta, Why’s that, heres code:

print("Test Started")
local Meta = {
	debounce = false
}
Meta.__newindex = function(SimplyNotMeta, ind, val)
	if(Meta.debounce) then return end
	print("Called __newindex, Setting value of table "..tostring(SimplyNotMeta).." at index "..ind.." with value "..val)
	Meta.debounce = true
	SimplyNotMeta[ind] = val
	Meta.debounce = false
end

local Meta2 = {}
Meta2.__index = Meta2
	
setmetatable(Meta2, Meta)

local SimplyNotMeta = {}
setmetatable(SimplyNotMeta, Meta2)

print("Setting val starting...")
SimplyNotMeta["Pizza"] = "Italy"
SimplyNotMeta["Taco"] = "Mexico"
SimplyNotMeta[1] = "Pizza"
SimplyNotMeta[2] = "Taco"
print("Setting val ended!")

It should print like “Called __newindex…” in output, but it isnt outputting this at all,
Please mention what could I improve here, Thank you!

1 Like

That’s because Meta2 and SimplyNotMeta are two separate tables. Creating a new value on SimplyNotMeta’s metatable would only activate the metamethods for Meta2. If you print out SimplyNotMeta and expand the table, you will see that __newindex is not directly inside SimplyNotMeta, but it’s within another table that is located inside SimplyNotMeta.