How do i make an :OnUpdate function for my module?

I have a module storing player’s session data (not datastore necessarily, also temporary data). Whenever that module gets updated I want scripts to be able to listen for that change. How should I do this? An example of what im trying to do is DataStore2’s onupdate function

1 Like


In case you still wish to use it for some reason…
Just have dsModuleObject:OnUpdate() bind to ds:OnUpdate().

meta.__index.OnUpdate = function(self, key, callback)
    self.DataStore:OnUpdate(key, callback)
end

I dont mean for the roblox datastore service i mean like if my module’s table changes. I was thinking of making bindable events that fire whenever I use the set method.

I don’t really recommend this but if you wish to do it, you can. Try doing something like you would in a language like JavaScript with jQuery.


JS:

$("p").on("click", function(){
  alert("The paragraph was clicked.");
});
$("p").on("mouseover", function(){
  alert("The paragraph was hovered over");
});

Lua:

moduleObject:AddListener("Property", callback)
moduleObject:AddListener("Data", function(object, newValue)
    print(object, newValue)
end)
--in the module when you change something:
if self.Listeners[prop] then
    for bound, active in next, self.Listeners[prop] do
        if active then coroutine.wrap(bound)(self, self[prop]) end
    end
end
--adding a listener
if self.Listeners[prop] then
    self.Listeners[prop][func] = true
else
    self.Listeners[prop] = {[func] = true}
end
--removing
if self.Listeners[prop] then
    self.Listeners[prop][func] = nil
    --optional clearing if empty
    if not next(self.Listeners[prop]) then self.Listeners[prop] = nil end
end
--clearing all
self.Listeners = {} --or nil

Ok just to clarify this is basically what my module is doing:

GlobalData = {}

--every time a player joins
GlobalData[playerinstance] = {}
GlobalData[playerinstance].Health = 1000
GlobalData[playerinstance].Coins = 1000
--

--then there is a set method
function set(plr, i, value)
GlobalData[plr][i] = value

--now after this I fire the relevant bindable event with new value and then any scripts listening to that will listen and run
end

So you’re saying I shouldn’t use bindable event? Also how do I use :AddListener() to replicate this behavior?

You would add a listener that listens for when Data is changed or for when any of the values inside of Data are changed. You can use those example functions however you want, because as long as you call them when you need them (and you call them properly) they will be functionally the same as bindable events without needing to create multiple instances.

Wait what is a listener? Is that a lua feature?

Literally read above… I wrote example code…

Sorry I read your code, earlier, im just still a little confused. Is the :AddListender() method supposed to be defined by my module?

Yes, those comments are representing functions and what their purpose would serve when you are dealing with listeners. AddListener would be the adding section, RemoveListener/DelListener/RemListener will be the remove part, and so on.

Alright I think i’ve done this properly. Just wondering, why not use bindable event though? It was a lot easier.

This is easier to manage and control imo and it does not require creating a separate instance. Bindable events are usually used when you want script-script communication (for whatever reason) and they also serialize the data that you put in them so you cant send things like objects (the metatable gets stripped) or mixed tables and such without error.