This tutorial will go over how to detect whenever a value inside a table changes. Pretty straight forward.
Before we continue any further. I recommend you are somewhat familar with MetaTables.
Also be aware that this tutorial will only work with dictionaries.
Now. To start. I guess you already have a table set up. For this tutorials sake i’ll call my table “Players”.
After you’ve created your main table. Create another one called “Proxy”
local Players = {}
local Proxy = {}
For demonstration. I’ll already set up some values. Make sure to write your values to the Proxy table. Not the main one!
Now we’re gettin’ into some deep waters.
To detect changes we need to add 2 metamethods. The methods we’ll be using are:
“__index”
“__newindex”
We’ll be giving our main table a metatable that will contain all these methods.
local Players = {}
local Proxy = {}
Proxy.Herobrinekid1 = 10 -- // For demonstration.
setmetatable(Players,{
__index = Proxy,
__newindex = function(Table, Key: string, Value: any)
end,
})
Now you might be askin’ yourself. Why are we using __index and __newindex. We’ll. I’ll try to explain it to you as best as i can:
__index
Were usin’ __index to link/connect our main table to the proxy one. This will copy all our values n’ methods inside the proxy table to the main table.
Whenever we try to write to our main table. Luau will check if the key(Herobrinekid1 in my case) exist in our main table which it doesnt. It’ll will then proceed to look if our main table has a metatable attached to it which it has. It’ll then look if we’re returnin’ a function or a table. In this case we’re returnin’ a table(Proxy) and return that one which contains our key. If none of those we’re found it’ll just return nil.
__newindex
Were usin’ __newindex to detect changes.
Normally __newindex fires when you try to set a value that’s nil. We can actually take advantage of this.
Since we’ve linked our main table to the proxy one. Whenever we try to change our keys value inside the main table(Which doesnt exist but does in the proxy one).
__newindex will fire because the key “Herobrinekid1” doesnt exist in our main table.
local MyTable = {}
setmetatable(MyTable,{
__newindex = function(...)
print(...)
end,
})
MyTable.Player = 1
Im terribly sorry if this explanation was really confusin’ or wasnt good. I hope you atleast understood some parts haha. Explainin’ how __index/__newindex actually works in this case really took a toll on my neurons.
Now we can begin’ to compare both the values. Old/New to check if a change happened. If a our value isnt the same as the new one. We can overwrite the old one.
local Players = {}
local Proxy = {}
Proxy.Herobrinekid1 = 10 -- // For demonstration.
setmetatable(Players,{
__index = Proxy,
__newindex = function(Table, Key: string, NewValue: any)
local OldValue = Table[Key]
if OldValue ~= NewValue then
Proxy[Key] = NewValue
end
end,
})
Now you’re basically done. This will register when a change happens in the main table.
Though if you also want to have the ability to add events such as Connect() follow this step. We’ll be usin’ a bindable event to make this easy.
local Players = {}
local Proxy = {}
local Event = Instance.new("BindableEvent")
Players.Changed = Event.Event
Proxy.Herobrinekid1 = 10 -- // For demonstration.
setmetatable(Players,{
__index = Proxy,
__newindex = function(Table, Key, NewValue)
local OldValue = Table[Key]
if OldValue ~= NewValue then
Proxy[Key] = NewValue
Event:Fire(OldValue,NewValue)
end
end,
})
Now you’re all set now you can connect a listener to your table to catch changes. Example:
Players.Changed:Connect(function(...)
print(...)
end)
Players.Herobrinekid1 = 5
task.wait(2)
Players.Herobrinekid1 = 10
Hope this was somewhat useful this was my first ever tutorial ive written here.
If you have any feedback make sure to tell me. All feedback positive/negative is very useful.