so im quite new to scripting in roblox and i wonder in which cases to use module scripts and when to use normal scripts or local
and i know that they are usfull for OOP but im unsure what are cons and pros of module scripts
would apreciate any help
ModuleScripts arenât necessarily better than other scripts, theyâre a very useful type of script that youâll benefit from using often. When it comes to Roblox scripting, itâs important to understand replication and how code communicates changes across the server â client boundary.
Replication is how a game engine ensures that data and everything that the players see on all of their different devices is the same, e.g. the position of all the characters, their animations, when a map changes or when a property is modified.
We have the server and then we have the clients. Every player that is currently on a game server has their client, which is the application window running on their PC. The server (in this case) is a remote device that everyone is connected to and where all of the changes/updates run through that will be forwarded to each client.
In order to keep gameplay secure, any changes made by scripts on the client do not âshow upâ, or replicate, to the server and other players (for example if a hacker injects code to delete the map, it will only show up on their client but other players will not notice). The only exceptions here are teleport/noclip/fling hacks because the roblox engine accepts any changes to the playerâs character location and velocity (read about network ownership).
There are the three kingdoms of scripts, first the two base ones:
- Script
- Typically these run on the server side, and are best placed under
ServerScriptServiceor objects inWorkspace.
- Typically these run on the server side, and are best placed under
- LocalScript
- Copies of these execute code on the playerâs client. LocalScripts only work when placed in
StarterPlayerScripts/StarterCharacterScripts/ReplicatedFirstas they have to be under an instance that is owned by the client.
- Copies of these execute code on the playerâs client. LocalScripts only work when placed in
A ModuleScript is unique in that it has the ability to be executed on both the server and client. This is because a ModuleScript by itself will not run any code until prompted to by either a script or local script. You can think of a module script like a reusable lego piece that can be accessed by any other script in your game (including other modulescripts!)
Something useful and important to know about a ModuleScript is that it has the ability to hold data that can be accessed by other scripts of the same type (client or server). For example, you can tell a module to remember the number â5â and other scripts that use the module can see this. You cannot use this to send code from the client to the server though.
A ModuleScript can be placed anywhere, and I like to put them in either ReplicatedStorage or ServerScriptService.
A common use of a ModuleScript is to make a utility module. This is a module script full of useful bits of code that you can repeatedly use whenever you like. It saves a lot of time and headaches as it prevents you from re-typing the same lines of code in many other scripts, especially if you end up needing to change that line of code.
Now thatâs a wall of words, time to show you a little example ![]()
Utility module examples
Hereâs a list of utility modules that Iâve created for myself in a particular game project.
A concrete example of how useful these can be is my Maths module. This is where I keep my custom functions that handle numbers and do useful number crunching.
Youâll notice when creating a ModuleScript that it starts with a single table variable.
![]()
This is because we can attach multiple functions / methods to tables. The bit of code below simply turns the Y of a the supplied Vector3 value to 0.

Another time saver I like to use is to have a library of references to commonly used Roblox services, like TweenService.
This means that rather than re-typing out multiple service getter lines of code for every script, I can type 1 line to require() this module and then access the services I need
Requiring my utility module scripts:

Using a reference of CollectionService to do something:
OOP through custom classes is fairly tricky as Luau doesnât have an official way to create classes like other languages such as C#. There are a few different ways to implement custom classes, and it gave me confusion around how the best way to do it is as many people advocate and argue for different paradigms. I wouldnât advocate for doing it as a beginner, but I donât want to dissuade you from trying
best to learn the basics first and work your way up!
A ModuleScript lets you store stuff like data, functions, variables etc that you can use across either server Scripts or LocalScripts. Theyâre not really better than either one, but theyâre useful and will save you lot of time
Best thing ever, actually. So many uses, like creating your own command library with ease.
thanks for your answer and examples and i would love to hear more from you about oop bc i wouldnt say im beginer scripter and im looking into other people solutuions about it
ModuleScripts make your life alot easier when dealing with more complex tasks. And makes modifying your system in the future alot easier too.
You can create âcustomâ methods, functions, and properties.
Vehicles, guns, and NPC systems. You should 100% use module scripts to do OOP.
Any system which requires you to access the same function across different scripts, or for example a system that has different properties and methods for certain objects (like vehicles with more or less maxspeed available) you should use OOP.
Alot of games have vehicle/gun systems and they have to update their game alot to add new vehicles and guns. Using OOP makes this alot easier whereas with a regular script with local functions not only would it look less clean but youâd probably have to change everything just to change one small little thing.
You can technically use attributes or instance values to create âcustom propertiesâ. But it would be slower to access an attribute or an instance value + unnecessary replication and wasted memory from instance values and attributes. With OOP you can simply store properties of an object on the server-side so the client doesnât have worse performance.
Iâm pretty sure the client is naturally weaker than the server because the client should only have been designed to deal with itself. Whereas the server needs more resources for complex tasks like replication + all of the clientâs tasks
i get your point but im a bit concerned aboud security of shared modules.
my point is that we can always asume that player send absolute garbage so how would i hide data or functions that player shouldnt see or use.
like would it be best to have 2 modules doing almost same thing or securing fuctions or data in one module
The client can see all LocalScripts/ModuleScripts it has access to, which is basically everywhere except in ServerScriptService and ServerStorage.
For systems than span both the server and client, itâs common to have separate ModuleScripts for each boundary. For example, in a data system where the server and client have their own ModuleScripts, theyâd both have :get() functions to fetch data, but :update() would be limited to the server.
if you really want to hide a piece of code/instance from a client, use serverstorage/serverscriptservice as those do not replicate to the client.
modules in their entirety are just a container for functions, methods, data etc.. yeah you can use them for the purpose of simulating OOP in an otherwise non-oop language.
basically one use case would be, you have a function you catch yourself using in lots of different scripts, then thatâs where a module would come in. you can put that function in the module, and now instead of copy and pasting the same thing over and over, you can just require the module and use it instantly while also making it easier to edit/maintain.
ur paranoia about the security of shared modules is a good thing
basically these 2 guys above me already got it, but one catch: shared modules should never be used for server authoritative logic. u should separate that into separate modules like PlayerManagerServer for server logic and PlayerManager for the client logic (this is how i do it in my games ). shared modules should always be treated as a public API, not a security boundary. yk ppl can still decompile the ModuleScripts and LocalScripts that they receive, right
? all logic inside of those scripts would be exposed, and obfuscation does nothing. it just does cool tricks, but that doesnât make it completely impossible to decode
. ServerScriptService/ServerStorage stores all instances on the server, which doesnât get replicated to the client, so thatâs why these guys reccommended u to use them
You can parent a module script in serverscriptservice and require from the server. The client wonât be able to access the code or the script at all.
At the end of the day some code will always be seen by the client like a local script is always visible but thatâs something you need to live with.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.


