Changing value not invoking metamethod __newindex but value is being changed

Hello, so I am creating a resource system where every resource will have its own seperate health n stuff like that but the problem is that, when I am trying to change the health after I created the object it isnt invoking the metamthod __newindex but weirdly enough it is actually changing the value though

local Resources = workspace.Resources
local ResourceManager = {}
ResourceManager.__newindex = function(self,index,value)
   if index == "Health" then
      print("Health value changed to "..value)
   end
   rawset(self,index,value)
end

function ResourceManager.new(Instance)
   local self = setmetatable({}, ResourceManager)
   self.Instance = Instance
   self.Health = 100
   return self
end

for _, model in ipairs(Resources:GetChildren()) do
   local newResource = ResourceManager.new(model)
   task.wait(1)
   newResource.Health = 0 -- This isnt invoking the metamethod
end

return ResourceManager

Alright so did a bit more debugging turns out that I have to actually put the __newindex inside of that actual self object then setmetable on it like so

Works as intended!

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