Hello, today I will be explaining how to effectively use a ModuleScript and explain to you new Scripters how it works.
Before getting more into this understand that this is mostly a beginners tutorial, you may already know most of the things I will be explaining, now onto the tutorial.
Firstly, a ModuleScript can communicate both to the Server and Client, this can be extremely effective. You can use a ModuleScript for many different things, storing tables, creating frameworks, etc… So let’s get into it.
I’ll be coding in Visual Studio, you can stay in Roblox Studio as it’ll be the same, however creating a Module you’ll start off with this.
On line 1 you’ve your Table, and on the bottom it’s returning that table, if you’re wanting to code anything in a ModuleScript mostly all your code will need to be in-between line 1 and the bottom line.
Now let’s first start off with creating arrays or storing things in our newly made Table.
So we’ve created hi inside our Table which equals to the string “ha”, we can require this module from a ServerScript or LocalScript and print hi so it’ll output “ha”. Let’s show you.
So we created a variable to require the module that I’ve put into my ReplicatedStorage and I printed out hi from inside the module in a different script. Yes! You can access the ModuleScript from a LocalScript or a ServerScript, and no you cannot access a LocaScript from a ServerScript and vise-versa. However, we input inside the print event
“module” is the variable name and the ModuleScript we want to access to grab hi.
Now understanding that we can access any ModuleScript from any Local or Server script now we can explain how to do the cooler things! Let’s create a function inside a ModuleScript!
So, this is completely different than the Functions we’re used to! Firstly to create a function we must define the table “module” then create our Functions name by adding a dot after the module. So,
function module.LALALA()
end
So now understanding that, inside the parameters which is in-between the “()” we put the letter J, which can literally be anything you desire however we will not get into Parameters as we’re focusing on this tutorial, though the Parameter J will define the string we’ll put when calling the function, let’s do that!
So now, the letter J inside our ModuleScript’s function will now define the string we’ve sent when calling the function “hi”, does that make sense? So now when we run the game it’ll print, “super delicious”.
So we’ve created a simple function inside our ModuleScript, imagine the many things we can use that for!
You can add many other features in your ModuleScript to make it super efficient in programming your game, it allows you to not only organize your code a lot more but help you store different aspects of what you wish to add into your game, it’s very efficient and effective and hopefully this small Tutorial helped you a lot.
If you’ve any questions please ask under this post and I’ll most definitely answer! Part 2 will come soon with a longer tutorial and more advanced to help you understand more about what ModuleScripts can be used for. Thanks again!