From what I am reading and to my understanding, module scripts allow your code to be reused like a function without having to be re-written as a function in many different scripts. The information returned from a module script can then be called into a normal script through require()
.
However, again, to my understanding, this is met with contradiction with how module scripts run only once. This means that if your variables in the module script “function” changes, the output is still the same which prevents normal scripts from getting different outputs when calling it again. For example, lets’s say I have a module script that detects the position of the player’s head, and returns that position. A normal script requires the module script and gets the position. All is well up to this point. However, after some time, the player moves and so does the position of the player’s head. Another normal script wants to get the position of the player’s head, so it requires the same module script. Since module scripts only run once, the returned data is the original position of the player’s head and not the new position.
So how do module scripts allow code to be reused without having to copy and paste it again. Isn’t it just a placeholder for a single value which will never change after it runs for the first time?
Unless, my interpretation of module scripts from various other forum posts and the developer wiki is wrong. I’m really excited about the prospects of module scripts that many have described of it, but I don’t see how it would work? Could anyone point out the error that I had made?
Thanks.
ModuleScripts don’t “run”. Instead, you reference them via “require()”, which allows you to call functions that exist within the ModuleScript. This is somewhat similar to global variables “_G”.
I’m still confused with this though. According to the Developer Wiki,
ModuleScripts run once and only once per Lua environment and return the exact same value for subsequent calls to require
.
Perhaps it’ll be easier to understand if you know how Global Variables work. Also, if you haven’t already, try this reference for an Intro to Module Scripts.
Unfortunately, I can’t seem to find an official reference for Global Variables, but they’re basically variables that can be accessed via any script (although, server/client variables are separate). The value of those variables will remain consistent.
_G.ExampleVariable = 'example'
Functions can also be stored and called this way. ModuleScripts are somewhat similar to this.
1 Like
Eh, I’m pretty sure module scripts are “reusable”. But if it doesn’t seem to work for, you can try having functions in your module script, so you can execute it multiple times. Here’s an example:
Let’s say this module script is called “ModuleScript” and is in ServerScriptService.
-- MODULE SCRIPT
local moduleFunctions = {}
function moduleFunctions.owo()
print("owo")
end
function moduleFunctions.uwu()
print("uwu")
end
return moduleFunctions
-- SCRIPT
local serverScriptService = game:GetService("ServerSriptService")
local module = require(serverScriptService:WaitForChild("ModuleScript"))
module.owo()
module.uwu()
module.owo()
If I did it correctly, it should print “owo” then “uwu” then “owo”.
I am aware that it isn’t really answering your question, but at least, you could use that as an alternative if you’re sure that module scripts can’t be reused.
3 Likes
@MightyDantheman The link on the Intro to Module Scripts was very helpful and answered my question. I can see why they didn’t make module scripts the way I mentioned it in my first post. It would definitely be more efficient to have many functions in a single Module Script table so that you don’t have to keep creating module scripts if you want to write another function. I now understand that module scripts involve tables that come as “packages” of functions for you to use all at once.
@lyookachu Thanks for your help as well, I think I’ve got the answer I’m looking for.