Skip to content

Instantly share code, notes, and snippets.

@depthso
Created January 9, 2026 10:38
Show Gist options
  • Select an option

  • Save depthso/dcd69b4a39f343fa4a8e68ff411dfcf1 to your computer and use it in GitHub Desktop.

Select an option

Save depthso/dcd69b4a39f343fa4a8e68ff411dfcf1 to your computer and use it in GitHub Desktop.
Detects changes within a table using a metatable with function callbacks for .Removed, .Added, .Changed
--[[
Created by @depso (depthso)
Example usage:
local Table = Module.new({
HiBlud = 67
}, {
Changed = function(...)
print("Changed", ...)
end,
Removed = function(...)
print("Removed", ...)
end,
Added = function(...)
print("Added", ...)
end,
})
--// Test data
Table.HiBlud = 41
Table.Gay = "Burner"
Table.HiBlud = nil
]]
type Table = {any}|{[any]: any}
type Callbacks = {
Removed: ((Key: any) -> nil)?,
Added: ((Key: any, Value: any) -> nil)?,
Changed: ((Key: any, Value: any, Previous: any) -> nil)?,
}
local Module = {
Removed = nil,
Added = nil,
Changed = nil
}
function Module.new(Table: Table, Callbacks: Callbacks)
--// Callbacks
local Removed = Callbacks.Removed
local Changed = Callbacks.Changed
local Added = Callbacks.Added
return setmetatable({}, {
__newindex = function(_, Key, Value)
local Previous = Table[Key]
--// Fire callbacks
if Added and Previous == nil then
Added(Key, Value)
elseif Removed and Value == nil then
Removed(Key)
elseif Changed and Previous ~= nil and Value ~= nil and Value ~= Previous then
Changed(Key, Value, Previous)
end
Table[Key] = Value
end,
__index = Table,
__len = Table,
__tostring = Table
})
end
return Module
@s-0-a-d
Copy link

s-0-a-d commented Jan 27, 2026

67

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment