How to set a function up with a module

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)

Module Script (Giving)

local module = {}

function module.onChanged(info)
return info

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.

That does look like it should work. Can you explain more the purpose of this?

Also, use this module structure:

local module = {}
module.__index = module

function module.SomeNameHere()

return module

oh I am quite suprised I was able to accurately guess the layout of how module scripts would send requests.

I guessed this from my knowledge of how Roblox Lua usually does things.

The purpose of this is for my recording module which allows you to record things in roblox experiences and then play those recordings back.

I plan to use this module for things like running and creating video ads or replaying final kills/good moments in my games.

I am also planning on making this module public once its done.

local module = {}

function module:onChanged()

return module

you are forgetting the important part of how to fire it.

for example in mine I called

function module.onChanged(info)
return info

module.onChanged("Hello") -- fire recieving script

How would I fire the function for your version

You would have to fire through the script. (the server script not module)

Well, here’s a quick rundown:

--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

--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

return myModule

I might have missed a few bits.
Here’s a post relating to dot and colon notation:
What is self and how can I use it? - Help and Feedback / Scripting Support - Developer Forum | Roblox

to clarify I want the module to be the one sending the info and the script recieving the info.

My module script will update a value, and when it updates the value, the script runs code with the new value, received from the module.

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 :confused: .

1 Like
  1. Variables:
  • isPaused: Boolean variable controlled by a server script, determining if the playback is paused.
  • Module script isPaused variable which is checked by the server
  1. Objective:
  • When the video completes playing, the server script should:
    • Set isPaused to true.
    • Automatically pause the recording.
    • Set the playback position to the start.
    • tell the server that the video is paused

Why not use a value instance, like a BoolValue? then, you could use .Changed or :GetPropertyChangedSignal("Value") to detect the change.

1 Like

I see so rather than using a variable it would use a Bool object value.

This may work let me see…

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.

Yeah, but a value instance would probably be better, and might even take less memory.

Just adding to what is here already.

local module = {}
local x = 100 -- a script global value

function module.functionName()
    local z = 200 -- a function global/only value

return module

And some documentation…
Intro to ModuleScripts

Module script:

local module = {}

function module.functionName(newval)
	return newval

while true do

return module

local script:

local module = require(workspace.module)

module.functionName = function(newVal)

So if I did this would it print “hello” ever .1 seconds on the local script?

Not quite…


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

return module


local module = require(path_to_module_here)
module:NameHere("Test value")

I am trying to get a bindable function behavior on the module.

for example remember the goal is that the local script prints hello every .1 seconds, which is fired by the module.

I suppose this is not possible but I saw datstore modules doing this

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:

local module = {}
module.__index = module

module.someCoolVariable = "something"

return module

and then:

local module = require(--[[path to module here]])
print(module.someCoolVariable) --> "something"

this reminds me of java but I am not a fan of the

module.__index = module

I think that looks pretty weird. I feel like you should be able to do

module.someCoolVariable = "something"

without having to do that. Its also pretty hard to memorize writing module.__index = module