I have heard from one of my friends that if I were to use the same function in almost all of my scripts, it would be necessary for me to store it in a ModuleScript and require it every time. But the question is: How would I try to do something like this?
You could try do something like this by having a module in replicatedstorage or somewhere where the scripts have access to it. the module can be something like the example below:
local module = {}
function module.MyFunction()
print("My function called")
end
return module
If a script would like to use the function it can do this:
local MyModule = require(path.to.module) -- Could be ReplicatedStorage or something where the scripts have access to
MyModule.MyFunction()
Thank you for your reply. I have a question though: Would this work if the function I would like to require has parameters needed to fill out?
Yes it would work.
local module = {}
function module.MyFunction(arg1)
print("A script called with " .. tostring(arg1))
end
return module
local MyModule = require(path.to.module) -- Could be ReplicatedStorage or something where the scripts have access to
local arg = 10
MyModule.MyFunction(arg)
Awesome! Thank you for your assistance. I am now a step further to proper game optimization.
No problem. If you have any other issues feel free to dm me.