Hello, I’m tring to make a function from a module script that first code on the receiving script. I’ve seen this functionality in datastore modules and I’m interested in how to do it myself.
For example it could be like this:
Script (Receiving )
myModule = require(Workspace.myModule)
myModule.onChanged = function(Info)
print(info)
end
Module Script (Giving)
local module = {}
function module.onChanged(info)
return info
end
module.onChanged("Hello") -- fire recieving script
return module
However since I don’t actually know how to do this, I doubt my current implementation actually works. and to clarify, I don’t want to use bindable events because they can affect performance.
--define the module
local myModule = {}
module.__index = myModule --set __index metamethod
--[[
Colon notation passes itself as an arguement
Dot notation does not
]]
--Init is a naming convention for subprograms that
--should only be run once as an initialisation
function myModule:Init(someInfo)
self.someInfo = someInfo
end
--now all your own subprograms can go below
--modules are especially useful in OOP because
--they can hold all the necessary items for a class
function myModule.NewClass()
local self = setmetatable({}, myModule) --this new table inherits everything from myModule
end
return myModule
Some kind of RBXScriptSignal is going to be needed here. I’m not too sure on how you could make your own RBXScriptSignal, but I’m pretty sure there is a way.
Otherwise, you would need to use a while true iterator to detect the change in variable and that would use a lot of memory .
now that I think about it rather than storing IsPaused on the server and the module script separately, I could set it on the module exclusively.
Then when I want the script to access it, I could use a getter method to determine the current value of IsPaused. That way when any changes are made on the module separately from the script accessing it, the script would save that state since it accesses the modules version not its own.
local module = {}
local x = 100 -- a script global value
function module.functionName()
local z = 200 -- a function global/only value
end
return module
local module = {}
module.__index = module
module.SomeValue = nil
function module:NameHere(newVal) --parameter "self" implicitely defined through notation
--return newVal - this makes the subprogram absolutely useless. Let's assign it to the module.
self.SomeValue = newVal
end
return module
LocalScript:
local module = require(path_to_module_here)
module:NameHere("Test value")
BindableFunction behaviour might not be necessary - if you edit a variable that is within the module, it will update for anything that requires it. So, if I had this: