Help needed how to detect when a key has changed in a dictionary

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("new key")
        dictionary[k] = v
    __index = dictionary
}) = "bar1" = "bar2" = "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