I need help detecting when a key in a dictionary has changed how can I do this, I’ve looked at previous examples but they did not seem to work resulting in an overflow error
Something like this should work
local dictionary = {
foo = "bar"
}
local newDictionary = setmetatable({}, {
__newindex = function(t, k, v)
if dictionary[k] then
print("changed")
else
print("new key")
end
dictionary[k] = v
end,
__index = dictionary
})
newDictionary.foo = "bar1"
newDictionary.foo = "bar2"
newDictionary.foo = "bar3"
Since the __newindex metamethod is called when a new key is added, you have to use a second table which doesn’t get anything added to it, it acts like a proxy.
Credit to Detecting when a dictionary is changed - #2 by Jxl_s
1 Like