Created
January 9, 2026 10:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --[[ | |
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
67