Hi,
I’m confused because many devs use module scripts. So how to use module scripts thoroughly?
Ok! here some recomendations and how to use module scripts:
create instance called module script and when you open script you will something like this:
local module = {}
return module
you can rename “module” to other name, for example:
local my_module_3000_cool = {}
-- you can add functions or some variables in module script
function my_module_3000_cool.COOL_FUNCTIONS()
print("it works!")
end
-- variables in module
my_module.abc = 3
return my_module_3000_cool -- <- dont forget to return this table!!
now you need require this code, create basic local script or script and type something like this
local my_module = require(path.to.module) -- <- require is a global function which allow you `import` your module script
-- calling this function from our module
my_module.COOL_FUNCTION()
print(my_module.abc) -- log: 3
Generic reusable chucks of code is always the goal when scripting.
Module scripts are like the final level at this.
Intro to Module Scripts
Basic ModuleScript Tutorial
How to use Module Scripts
Discussion of best-practices
Performance considerations
What Are Module Scripts
How to Use ModuleScripts
Module Script Overview
And one of my go to all pros..
Learning ModuleScripts – AlvinBlox
first require this module script from somewhere else, a common approach for using modules are having 1 server script and 1 local script and that requires all modules scripts thats inside of server script service (server script) and starter player scripts (local script)
local module = {}
-- Functions
function module.FunctionName()
end)
-- Initiate
module.FunctionName() -- can call here or from another module script
return module
have to return the module to work when another script is doing require(module path of this module)
At a high level, module scripts are used to simply return a table. Here’s a basic example of a module script named MyMath.lua:
local MyMath = {}
function MyMath.Triple(n)
return n * 3
end
return MyMath
It returns a table, and all the table contains is a single function called Triple.
An example of using this module:
local MyMath = require(script.MyMath)
local applesEaten = 5
applesEaten = MyMath.Triple(applesEaten)
print("applesEaten:", applesEaten)
You use module scripts anytime you want to make your code more organized into distinct sections. If, for example, you have a script 2,000+ lines long and it has dozens of functions all in that one file, then you could move some of those functions into a separate module script. This is extremely useful for organization.
Minimize their use as much as possible
Due to Luau having no linker they are pretty bad for performance
Use them only when really nessesary
That’s just straight up not true.
They could return anything, and in your case, returning a function makes more sense.
return function(n:number):number
return n * 3
end
Anyway, don’t do that either; it’s not inlinable, althrough not as bad as your first example.
No lol, don’t ever do that
That just bloating game’s performance and nuking “true” organization that does not stab performance in the back
Don’t use module scripts. Regardless of what propagandic slop is forced on you, don’t use them unless they’re explicitly for static storage
What’re your sources on module scripts affecting performance to such a degree? I’ve never had any concern before with module scripts.
I am aware module scripts can return things other than tables, but as far as I’m aware, tables are the classical use for module scripts — since this is in reply to someone who’s not even familiar with module scripts at all, I didn’t think about mentioning things outside of the most common usecase.
Honestly, he’s one of the more knowledgeable devs on the forum, but for whatever reason, he can’t stand module scripts.
I haven’t checked out his resources, but he’s made a lot of community resources, so I’m sure he’s had a multitude of encounters with module scripts, but even if he doesn’t use modules in his resources, he’s definitely used them elsewhere.
I’ve never understood his hyper-performance approach, but it won’t matter for 99% of projects, so a module-based architecture has worked fine for me.
I do this all the time..
.. When I’m done, I’ll put them all in one spot.
I’ve never understood his hyper-performance approach, but it won’t matter for 99% of projects, so a module-based architecture has worked fine for me.
Take a look at a few of Roblox’s premade templates .. extreme use of module-based architecture.
That race template is a thing of beauty, 100% module-based architecture.
Ah, interesting. Didn’t know that about him; I’ve not spent much time on this forum.
It’s certainly the case that fixations on performance really aren’t justified in the vast majority of cases — and I say that as someone who tends to really try to optimize everything.
As Knuth’s famous adage goes: “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.”
Thank for replying me. It’s useful
amp out here with a W take like always