Metatable's index metamethod not working

So I have this metatable and neither the newindex nor index metamethods are working

local plrStats = {
	["comboCounter"] = 0,
}

local plrStatsProxy = setmetatable(plrStats, {
	__index = function(self, key)
		print(key)
		return rawget(self, key)
	end,
	__newindex = function(self, key, v)
		print(key, v)
		return rawset(self, key, v)
	end,
})

plrStatsProxy.comboCounter += 10 -- doesn't print out anything

Any help is appreciated

I’m still in need of dire help, I have tried using the newproxy method but I need it in a form of a table so this unfortunately won’t work

__index and __newindex are only fired if the element does not already exist in the table. You’re going to need a proxy table if you want the behavior that you’re expecting

The majority of scripts emulate these metamethods (table index/table assignment) via getter/setter functions.

local plrStats = {
	["comboCounter"] = 0,
}

local function getCombo()
	return plrStats.comboCounter
	--Add additional code here.
end

local function setCombo(valueIncrement)
	assert(type(valueIncrement) == "number")
	plrStats.comboCounter += valueIncrement
	--Add additional code here.
end
1 Like