Hooking Event to ModuleScript variable?

  1. What do you want to achieve?
    I’m hoping to find a method to accurately detect a change in value of a ModuleScript to update a “progress bar” for a cooking system. Optimization is key and I’m open to new ideas.

  2. What is the issue? At the moment of typing, I’m currently struggling to find any means to get a value to update and trigger an event on the client automatically, all without being forced to use a RemoteEvent (which I’d rather avoid due to clutter, but it may be necessary) or do it on the server. RemoteEvents in particular are also questionable in this use case, due to the frequent update rates that make cause throttling or lag, especially in larger servers.

  3. What solutions have you tried so far? I have searched but have unfortunately not come up with anything.

This is a very basic function I have set up in my module to permit the server to change a cookTime variable whilst permitting the potential firing of an event that the client can read.

function cupModule.increaseCookTime(amount)
	cupModule.currCookTime += amount
end
1 Like

So what you have is called a setter function, which is a function designed to change a value there are ways to optimize this like having a general setter function for any value for the player; to connect variables to events though you would need to use metatables; so here a pretty basic example

local DataTbl = {
    Gold = 100,
}

setmetatable(DataTbl,{
    __newindex = function(_,index,value)
        -- do stuff after value changes
        print(index,value) -- Gold 200
        RemoteEvent:FireClient()
    end
})

DataTbl.Gold = 200

This is a very basic example of setting one up and there are downsides to this method; to prevent clutter with remote events just simply use 1 remote event and have all the data inside of a table

I personally recommend sticking to the setter function method since its easier to setup/manage and is the method I personally prefer

I could theoretically hook a bindable through this method though, couldn’t I? This code is contained within a module the client can relatively easily access, which I’m hoping it can directly update from.

A bindable only helps with same machine communication so from Server → Server or Client → Client; if you want that then yes you can use bindables or create your own bindable system; however since you mention guis that should be a Server → Client relationship and hence should use remote events

Keep in mind that server changes onto a module script will not automatically replicate to the client

1 Like

Suppose I’ll stick with the remote method, thank you!

1 Like

As you stated getters/setters are more practical for this as opposed to creating an assortment of RemoteEvents, RemoteFunctions, BindableEvents, BindableFunctions.