Is there a benefit to using ModuleScripts over LocalScripts?

Looking back on some open-sourced games I look back at to learn more about development and Lua, specifically speaking about @berezaa’s games. I’ve noticed that compared to LocalScripts in both Miners Haven Open-Sourced and Vesteria Open-Sourced there are more likely going to be ModuleScripts. Is there an advantage to this? Why am I seeing more developers favoriting Modules compared to LocalScripts.

Please let me know, I’m genuinely quite curious :slight_smile:

  • eiz_a
1 Like

I think it’s mostly preference, some people find it more organized if you have many ModuleScripts and only require them in your MainScript.

ModuleScripts allow you to use them in multiple scripts, which is pretty cool.
I myself don’t really use them, but for the sake of making my code “cleaner” I should do that in the future.

Also, people who know OOP usually do that in ModuleScripts, although you can also use OOP in Local/ServerScripts.

1 Like

So, let’s go over what each script entails:

-Script is the default server script, the script that is most useful, as it conveys changes to all clients instead of simply conveying changes to one. This is the script that you will use most throughout your time as a Roblox developer.

-LocalScripts convey client sided changes, changes that will be visible to the client the script is being executed on only. This means that you only really use them when making UIs and possibly raycasting and the sort. These have access to player cameras, user input (keyboard, mouse, etc.), and stuff that a client takes care of

-ModuleScripts are the stuff in question. For that, think of module scripts as a giant storage room. Module scripts, when created, start with the following lines of code:

local module = {}

return module

You can kind of think of the first line as initializing the storage room, and return is the door. Then, you can define variables like so:

module.Variable = "This is a variable"

You can also define functions:

function module.CoolFunction()

end

So in other words, ModuleScripts can be used both on the client side and the server side, and you should only really use them when you need to take data from somewhere, or you find it more organized to use this sort of method of organization.

2 Likes