Module script help

Modules exist to easily change code for scripts that require it, at least thats as much as I know.

I am just getting into modules, simply because I want to be able to update code easily for scripts I will be using extensively.

I want to be able to have a function, and within that function have the ability to call other functions in the module script.

I don’t think this is how module scripts work, but I need to be able to easily update code, but not be limited to just one function and nothing else.

I had tried variables in the script that requires the module, but like I said before, I don’t think it works that way.

Basically, a proper script that has the ability to call other functions within said script, and store global variables, but acts like a module script so I can easily update code for all scripts that require it.

I’m not sure I understand the question, are you asking whether modules can call functions within themselves? If so, then yes:

local module = {}

function module:function1()
    self:function2() -- or you could type module:function2()
end

function module:function2()
    print("Second function")
end
return module

The purpose of module scripts isn’t simply to “easily change code”, modules are the key to writing code cleanly and clearly though Object Oriented Programming. I’d recommend reading this tutorial: All about Object Oriented Programming

1 Like

Yes, but also where to store global variables for both functions if possible.

1 Like

Is this what you mean?

local module = {}
local val = 0

function module:increment()
	val = (val + 1)
end

function module:decrement()
	val = (val - 1)
end

function module:getVal()
	return val
end

return module
1 Like

Please edit your post and it word it better. Half of it makes no sense.

You store global variables in the global space. If you want to change these from outside the module use getter and setter functions.

You can just do this

local module = {}

function calculateNumber()
    return 2+2
end

function module.getNumber()
    local calculatedNumber = calculateNumber()
    local newNumber = calculatedNumber + 5
    return newNumber
end

return module

A module works exactly how a local script would, except if you want to call functions outside of the module you’d have to call them “module.BLABLABLA” inside the module and run them like “requiredModule.BLABLABLA()” in a localscript.

@opplo I think his post is pretty clear

Something funky you might like to do is this.

So script one

local Table = {}

require(Module1)(Table)
require(Module2)(Table)

Module 1

return function(Table)
Table.Test = 'a'
end

Module 2

return function(Table)
print(Table.Test)
end

→ ‘a’

If you want to share a lot of environments, you can do some really cool setfenv and getfenv stuff