I am trying to make a metatable that will detect when a value is changed. Is it possible?
This is what I have wrote so far but the __newindex does not run when a value is changed inside of a table
local dataMetatable = {
__index = function(t, userId)
local newEntry = {}
rawset(t, userId, newEntry)
return newEntry
end,
__newindex = function(t, userId, value)
if type(value) == 'table' then
local playerMeta = {
__newindex = function(innerTable, key, value2)
print('key = '.. key)
if key == 'Diamonds' then
print('Diamonds value changed for user' .. userId.. ': ' .. tostring(value2))
UpdateDiamondLeaderstats(userId)
elseif key == 'Level' then
print('Level value changed for user' .. userId.. ': ' .. tostring(value2))
UpdateLevelLeaderstats(userId)
end
rawset(innerTable, key, value2)
end,
}
setmetatable(value, playerMeta)
end
rawset(t, userId, value)
end,
}
playerData.PlayersData = setmetatable(playerDataTable, dataMetatable)
I have been looking at old posts and they dont seem to really help. I have also seen some things saying that this is not possible. I dont want to remove the table and add it back with the new value either.