Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

When and where should I use module scripts?

Asked by 4 years ago

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!

2 answers

Log in to vote
1
Answered by 4 years ago

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

0
Thanks for answering my question! I was wondering if you could show me an example of how you would use a module script for a tool. For example: punches, swords, or guns. Add me on discord @ Brandon#4128. Brandon1881 721 — 4y
0
Glad it helped you, I couldn't add you on discord since the add friends thing rejected me. I think you have maybe changed your tag or soimething like that. I can provide a short answer though, if your making a gun, you have can have functions in the module script like (shoot. reload, scope etc) and have input coming in from a LocalScript then executing those functions. 123nabilben123 499 — 4y
0
Also it's never good to use a module script for the client since if the client is a hacker they can self-require it and execute whatever they want. So you should have all the input and gun functions in one localscript. 123nabilben123 499 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

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!

Answer this question