I've been having a tough time trying to understand when and where I should use module scripts. I know how to use them like for example,
local module = {} function module.printSomething(text) print(text) end return module
and to call it:
local module = require(whereeveritis) module.printSomething("Hello World!")
I've heard they're good for things like bullets or swords (that do the same thing) and I've looked at some module script examples as well. However, I was really confused as when I did not see any sort of script inside a tool, there was only a module script inside it. If anyone could explain it would be greatly appreciated!
Modules are used for Object Orientated Programming (OOP) which is used for a way of programming and making “classes” that have certain things. You can manipulate, change, swap and create objects and think of these as values that “come alive”.
You have been doing OOP your whole life in roblox actually, changing a part’s colour is OOP, your manipulating custom created objects and treating it like it has value. OOP should only be used for things that can be saved time on or organized upon. Writing OOP for everything is not necessary as it can lead to disorganization.
Modules are also used for code organization, pretend you have a map changing script but it is more than just a basic one, it teleport’s players, give players tools, and changes the teams.
You might want to have a module script containing functions to do tasks with full customizability, say for the example a “give players tools” function can have a parameter containing a table of all the “gear” that you would use. This is also known as a library.
Modules can be used for sharing code, as if you publish a module and someone gets the ID of it they can execute whatever in the module script that they have required on.
Modules should never be used on the client, hackers can self-require the module and execute whatever piece of the module they like, Modules are most of time used on the server anyway.
Modules run in their own environment. Say player A has a part in workspace and player B doesn’t but they execute their own modules to find the part, player A returns true but then player B returns false. Running in a server environment is the same way, a server based module can access the ServerStorage while a client based module can’t.
Also about how you found a module script with no actual script inside of a tool, I think that either the creator forgot about the tool, or they want you to fully customize it using the module script since a modulescript cannot run without a non-modulescript (LocalScript or script) executing it.
I hope you found this useful and if you have any questions you can ask me
Module scripts are mostly used as a library you can access from different places. For example, you could have a module that stores information about enemies in an RPG game, such as health, speed, what it drops, and so on. Here's an example of what a module script for this scenario would look like:
local module = { Enemy1 = { Health = 10, Speed = 16, Gold = 2 }, Enemy2 = { Health = 25, Speed = 14, Gold = 5 }, Enemy3 = { Health = 50, Speed = 12, Gold = 10 } } return module
You can also put functions in a module that you can call from different places as well. Here's an example of this:
local module = { add = function(a, b) return a + b end, concat = function(a, b) return a .. b end } return module
If you have any questions or problems with this answer, let me know. I'd be happy to clarify or fix it.
Hope this helps!