Understanding ModuleScripts?

I’m coming to the clear realisation that ModuleScripts are an absolute god send in terms of organisation.

I have some questions regarding them?

image


Questions:


The image above displays my ‘Starship’ layout and how it’ll work.
I’m wondering if, using ModuleScripts, I can do the follow:

  • Store all Functions in one rather than requiring the other Modules via each Module. So initially, “Function” will require all of the other ModuleScripts; but in order to re-contact a Function within “Function” would I have to require it again?

  • As shown above, I have different Models: “Exterior” & “Interior”; as well as these, there will be a Folder / Configuration dedicated to managing Values - Such as ObjectValues, BoolValues etc.
    Currently, when transferring Data between the ModuleScripts and other Functions I have to send them through as Arguments: “Flight = function(Exterior, Interior, Values)”; However, is there a way I can avoid this and have all of the necessary arguments stored in one area and can be called from there?


I’m really intrigued by the uses of ModuleScripts and look forward to enhancing my knowledge on the subject.
If any examples can be provided, I will be grateful.
Thank you!


1 Like

Once you require a ModuleScript in a script or another ModuleScript, you don’t have to require it again. However, if you would like to use that module script from another script then you would have to require it again.

-- script (any)
local module = require(script.Module)
module.log("Hello World!")
-- module script
local module = {}

function module.log(...)
    print(...) -- print whatever what put in the parameters
end

return module

You could possible just do something like this

-- module script
local module = {}

-- make the module script get the models once so you don't have to send it over again
local exterior = script.Parent.Parent.Exterior
local interior = script.Parent.Parent.Interior

function module.Flight(values)
   -- do stuff
end

return module

That’s a way I’ve trialled before -
Is there any way I can create a ModuleScript PURELY for storage of reference and values?

So main script initially creates these references;

local Exterior = Whatever
local Interior = WhoEver

then passes them into the Storage Module - From this, all I’d have to do is require the Storage module and call the function to recieve what I’m looking for?