Module Scripts - Roblox Advanced Scripting #11 — Transcript
Full transcript
- 0:00you ever had those instances when you were writing scripts for your Roblox game and you
- 0:05pretty much had to reuse a lot of code that was from one script into the next let me show you
- 0:11something that I've written here so I have this function that I don't even know what the heck it
- 0:16does but let's say I wanted to use this in a different script the only way I would be able
- 0:20to use this in let's say this script is I would literally have to take my function and I would
- 0:26have to paste it into my new script so that I can use this function inside of this script
- 0:32and not this one so this is already a problem because duplicate code is never usually a good
- 0:37thing to have in your scripts because it could be very disorganized very quickly especially if your
- 0:42games become a lot more complex in the future we might even do the same thing with our third script
- 0:47here I would just copy and paste the same exact function because there's no way we can access
- 0:53these functions outside of the script that the function was created in and not just functions
- 0:58but also variables that we want to access in other scripts and not just the one that it was created
- 1:04in this is pretty much where module scripts come in as part of what we're going to be talking about
- 1:09in this episode to help us with this problem but before we start implementing these module scripts
- 1:14let's take a little bit of a deeper dive into what these module scripts actually are all right let's
- 1:19think about this for a minute so so far in our Roblox development Journey we've been able to
- 1:24create scripts that run on the server and we've been able to create local scripts that run on the
- 1:29client also known as our player so these two types of scripts are very important but now we're going
- 1:35to be introduced to a third type of script called a module script so you might ask what the heck
- 1:41is a module script and why is it useful a module script it's essentially a script that allows you
- 1:47to access its contents outside of that script itself to any other scripts that's inside of
- 1:54the game so let's say I had this function called change base plate color instead of only being able
- 1:59to access this function inside of the scripts that it was created in I want to access this function
- 2:05inside of other scripts without having to recreate the same exact function that's one example another
- 2:12example would probably be a global countdown timer for if you're playing a round based game
- 2:18uh you want to be able to access that variable in multiple scripts not just the script that
- 2:23it was created in and so in my opinion those are the two most useful cases for using module
- 2:29Scripts it's to access information outside of that script that created that information all
- 2:35right but now let's create our module script and something that's actually really interesting about
- 2:40module scripts is you know how with scripts it can only be accessed in the server with local scripts
- 2:45it can only be accessed in the client but for a module script it can actually be it can actually
- 2:50be accessed by both the server and the client so that's something really interesting to know about
- 2:56and because we can access this module script from both the server and the client we might say that
- 3:02we want to put it inside of replicated storage and for the sake of this example we are going
- 3:06to create a module script inside of replicated storage but you can basically create this module
- 3:10script anywhere it can be inside of replicated storage it can be inside of server script service
- 3:15server storage starter pack starter player it can you can pretty much have it anywhere depending on
- 3:20how you want to use this module script but for the first time we're going to create it inside
- 3:24of our replicated storage so let's hit the plus sign and let's create a module script which looks
- 3:30like this and you might notice that hm this looks pretty interesting so we have built-in code here
- 3:36that's not just our simple hello world print statement that we usually have in our scripts
- 3:41so let me try to give you some insight into what this syntax means so let me just scroll so let
- 3:47me just zoom in a little bit so we have two lines here we have a local variable just simply called
- 3:52module and this is a table and we're basically returning this empty table that the script has
- 3:58already created for us so what does this mean this pretty much tells us that a module script
- 4:03in reality is basically just a table and you can put whatever information you want inside of this
- 4:09table and the script will return this table that we created full of information that we
- 4:15put down inside of here and so if we want to use this module script we have to call it inside of
- 4:21a different script and then use whatever whatever is inside of the content of this module script so
- 4:29if that part didn't make sense to you let's start putting information in this table and let's fetch
- 4:33this information inside of a different script so because this is a table we can put whatever
- 4:39information we want inside of this table so let's drop a line inside of our module table and let's
- 4:44make a variable let's call this variable my number and we're going to set this equal to some number
- 4:50which we'll probably just say 10 and we will separate this with a comma because this is a
- 4:54table full of multiple entries and let's drop a line and create another entry so so we're going
- 5:00to call this one my string equals hello world or something like that so now we have two pieces
- 5:08of information inside of our table and of course we're not going to use a local scope because this
- 5:13is a table full of information so essentially now what we're doing is we put an information inside
- 5:19of our table and now we're going to return this so now how do we access this information outside of
- 5:25this module script we're going to do that by using a function called require so let's create a new
- 5:32script let's go to server script service let's hit the plus sign and create a new script let's rename
- 5:37the script to module call let's delete this here and we're going to make reference to our module
- 5:44script inside of replicated storage now this is a beginner mistake that a lot of people make
- 5:49what you probably would do is just simply make reference to the module script itself just by
- 5:54saying local module script equals game. replicated storage do module script and you might think that
- 6:04okay so now we have our module script we can just access the information that's inside of
- 6:07here this is not the case because we have to use a specific function to be able to take whatever
- 6:14is inside of this module and get that module when this returns it we have to essentially wrap this
- 6:20reference around inside a require function so it's going to look like this we're going to say require
- 6:28and then open parentheses close parentheses this is what the this is what it's actually going to
- 6:33look like because if we did just game. replicated module script it's referencing the instance but
- 6:39what we really want is the actual table full of the information that we're looking for so
- 6:45now what we can do is we can basically just write a print statement and we can directly access our
- 6:52variables that are inside of this module table so we can say modules script. my number number and we
- 7:00can also access the module script my string as well so if we hit run then we should see
- 7:09our change in the so we should see in the output that we printed the number 10 and we also printed
- 7:16hello world because we successfully made a module script table that has variable information and we
- 7:23required that using a different script to then get the information that we wanted and print it
- 7:29out and that's the really cool thing about module scripts we can create stuff inside of this module
- 7:34and access it in any other script we can even do the same thing with a local script let's say in
- 7:39starter GUI we had a local script we can pretty much do the exact same thing inside of our local
- 7:46script now of course this is only going to work if I'm actually playing the game but we're not going
- 7:50to do that so that is the cool thing about module scripts there's also one quick little thing I want
- 7:56to note is that a module script doesn't need to only contain a table full of information or
- 8:02functions to then return this uh to then return this module to uh whatever script is going to
- 8:08fetch this module we can even have it execute code if we decide to actually fetch this module script
- 8:17so what I'm talking about here is that outside of our actual module script table we can do regular
- 8:23stuff inside of this module script as if it was a normal script so we could pretty much just say
- 8:28print module script has been activated something like that now the the problem with this is
- 8:37that if we decide to just run this on our own um without calling it so if we just run it this print
- 8:44statement is not going to run it's just going to print some examples that I had previously don't
- 8:48worry about that but in essence this this print statement is not going to run because there's no
- 8:55script that required this module script that we created yet so in order to print this or any code
- 9:01that we want to execute we need to First require this module script that we created so let's create
- 9:06a new script over here and let's make reference to our module script by saying local module script
- 9:14equals require game. replicated storage. module script and this is basically all we need to do we
- 9:22just need to require game. replicated modu script and then what would happen is that as soon as the
- 9:29script requires it this is going to run print module script has been activated so now let's
- 9:34see the game inside of our output when we call the module script so it has now said module script has
- 9:42been activated because now we have at least one script that required the module script inside of
- 9:48replicated storage so not only can we do print statements we can pretty much do anything like I
- 9:53mentioned as if this was a normal script we can do a print statement we can make computations we can
- 9:59do all sorts of stuff that isn't just information inside of this module script table so that's a
- 10:05little something I wanted to throw in there in case you were curious about that now let's try
- 10:10adding a function to our module table and there's two ways of doing this I'm going to show you both
- 10:14ways so the first way is to do it directly inside of our table so we're going to put another comma
- 10:22after hello world to add another entry let's drop two lines and this is how we're going to create
- 10:27a function so we're first going to put in the name of our function which we'll just call my
- 10:32function and we're going to set this equal to a new function so it's basically just going to
- 10:37look like this open and close parentheses and then hit enter and Roblox will automatically put in the
- 10:43end statement and also a comma to signify that we can put in another entry down here um after
- 10:50this function but we're not going to add a new entry right now so now we can do whatever we want
- 10:55inside of this function so we can just write a simple print statement this is is a module script
- 11:01function just something as simple as that this is one way of doing it and we can access this in our
- 11:08module call script by simply going down here and we can just simply say modules script. my
- 11:16function open and close parentheses this did this does not take any parameters and we don't have
- 11:22to wrap this inside of a print statement because this is already going to print something if we run
- 11:27the script so let's hit run and see our output so we can see that it has successfully printed
- 11:35this is a module script function because we made a function inside of our module table and we called
- 11:41it outside of here using this external script so that's one way of creating this function there's
- 11:47another way of creating it and I think this is the more beginner friendly way of creating this
- 11:52function so let me show you what it looks like let's delete this function right here and let's
- 11:57drop two lines outside of this module table so we're not going to directly put information inside
- 12:03of this table but we're going to put information in the table outside of the actual table so this
- 12:09is what it's going to look like we're going to Simply say function because this is how a
- 12:13function is declared and we're going to put in our module table name which is just simply module so
- 12:20we're going to say module Dot and whatever our function name is which is my function and then
- 12:27open and close parentheses enter and we can do the same thing here with print this is a module script
- 12:36function this essentially does the exact same thing we just did with the first way of creating a
- 12:42function in a technical sense there is a third way of writing this function as well just by saying
- 12:47the module name. my function and we set this equal to a new function open close parentheses enter and
- 12:53we can just copy the same exact thing over here all of this basically does the exact same thing
- 12:58it depends depends on your preference which syntax you like to do the most and you might wonder does
- 13:03this only work with functions absolutely not this can work with pretty much any variable you can
- 13:08think of so we can pretty much have this as modu doy number equals 10 this essentially does the
- 13:16exact same thing as this you can add information to this table directly inside the table or you
- 13:22could just do it out here as well so we can delete everything we had here we can and drop another
- 13:30line down here by saying module. my string equals hello world this pretty much does the exact same
- 13:37thing it all depends on what syntax you like to do what looks the most organized to you and if you're
- 13:43generally comfortable with the way you write these module scripts I also do want to add as a layer of
- 13:48information is that module scripts can only return one value that's why generally we're sticking to
- 13:55just using one table which is our module local variable because this is a table that can store
- 14:02so much information that we can just wrap this inside of one variable and return that so that's
- 14:09generally why this module script is structured the way it is because a module script can only
- 14:14return one value so why not have it be a table that can store multiple bits of information to
- 14:20make it worthwhile so that's something I wanted to sprinkle in there here's what we're going to
- 14:24do let's take our module script and let's make it into a basic utility script that acts as a basic
- 14:31calculator for adding subtracting multiplying and dividing two numbers that we pass into it so this
- 14:37is what we're going to do let's rename our module table you don't have to do this but I just think
- 14:42it's more convenient to do it this way so that we actually understand what this module script
- 14:45does we're going to call this basic calculator and we're going to return this basic calculator table
- 14:54down here as well let's remove these two lines over here and let's let's write four functions
- 15:00for adding subtracting multiplying and dividing so it's going to look something like this let's
- 15:04say function basic calculator. add and then in these parameters we're going to put in our first
- 15:12number and our second number hit enter and we can pretty much copy this three more times for
- 15:19subtracting multiplying and adding so the second one is subract the third one is multiply and then
- 15:27the fourth one is div F and let's make a result variable for these calculations so we're going
- 15:33to say local result equals num one and obviously adding you're going to use the plus sign num two
- 15:41and we can pretty much do the same thing with this down here but instead of adding we are subtracting
- 15:47and over here we're going to multiply using the asterisk symbol and then for dividing we're going
- 15:52to use the forward slash and now what we'll do is return that result we'll just simply say
- 15:57return result and we can just copy this and put it down here to all of these other uh functions
- 16:04so this is a simple really basic calculator that we created that we can use outside of this module
- 16:11script now so let's go into our module call and let's rename our module script to basic calculator
- 16:19and on the right side let's actually rename our module script to basic calculator as well and
- 16:26we're going to change this reference to basic calculator let's remove these lines over here
- 16:32okay and so now what we'll do is we're going to create a new variable we're going to call this one
- 16:37local add result equals basic calculator. add open parentheses close parentheses we're going to put
- 16:45in two arguments here so we can probably say five and five for two of our numbers and let's print
- 16:51that result add result we can do the same thing with subtracting multiplying and dividing as well
- 17:01so this will be the sub result this will be the multiply result and this will be the Divide result
- 17:09and let's do this really quick uh for subtracting we can probably do five and five multiply five
- 17:17and five divide five and five yeah I think I think we have it pretty much sorted out and
- 17:21we're also going to change the function call to be subtract this one's going to be multiply and
- 17:29this one is going to be divide okay so now let's hit run and see our results in the output so we
- 17:36can see that the first result is 10 because 5 + 5 is 10 the second result is zero because 5
- 17:42minus 5 is 0 25 because 5 * 5 is 25 and then 1 because 5 divid 5 is 1 so our utility uh class
- 17:51or module script basically works as expected so now we can access this module script with
- 17:58these functions in any script that requires this module script that we created at the very top so
- 18:05that's pretty much the barebone basics of what a module script is if you can understand this then
- 18:10you'll be set for really organized scripts inside of your Roblox games for the future to come and
- 18:17this is definitely something you should take into practice and consideration when you try to create
- 18:23your Roblox games because it is tempting to just copy and paste the same uh function or
- 18:28variable over and over again but there will be times where module scripts will come in
- 18:33handy and will actually be necessary as well when you need to fetch Global variables that
- 18:39you need to use in multiple scripts and variable functions and all of and all that sort of stuff so
- 18:45in essence a module script is a table that can be accessed anywhere inside of your Roblox game it's
- 18:53a shared script that has information that any script can access and can use so that you don't
- 19:01have to copy and paste the same stuff over and over again and not even just copy and paste but
- 19:06sometimes you will need the same variable or same function or whatever it is in your other scripts
- 19:12without having a copy of that instead but before I end the video I'm going to show you a couple
- 19:17examples of utility scripts that I've used in the past to to show you what they actually look like
- 19:23when they get more complicated okay so there's two scripts that I want to show you that act
- 19:28as utility scripts that have been created by the Roblox community so the first one I want to show
- 19:33you is a simple binary search tree uh module with functions that can be used in other scripts so
- 19:40it's going to be here and I want you to note to not pay attention to the contents because it can
- 19:47get overwhelming pretty fast I want you to focus on the the module script syntax so don't even pay
- 19:53attention to the content just letting you know about that so we first have the Declaration into
- 19:58our module table which is just binary search and then we have a function call that takes into two
- 20:03parameters that's just default compare and we have another function here and we have another function
- 20:09declaration here another function declaration here and that is basically the whole script so
- 20:14this was a pretty simple example uh but if you don't know algorithms then of course this seems
- 20:19very complicated to you but in terms of the module script syntax and structure this is a pretty basic
- 20:25one now let's look at a little more complicated at one which is a custom data store framework and
- 20:31this one is by encoded Lua back in 2018 this one is a bit lengthy but it is very interesting to see
- 20:40what you can do with module scripts so over here we have a whole bunch of tables that we're going
- 20:45to be using inside of this one module script we have a configuration table specifically for the
- 20:50developer if you want to just change a bunch of stuff here um and then down here we have even
- 20:56more tables we have function declarations more function declarations another function declaration
- 21:01another function declaration another one and another one and that is basically the rest of
- 21:09what this module script has to offer and then of course we also have a bunch of stuff here that's
- 21:15running in the background that the module script does not have and then it just simply Returns the
- 21:20module as expected so the similarity with both of these module scripts is that they both follow
- 21:25a similar structure or syntax because obviously they're both module scripts I want you to get a
- 21:30feel for how these module scripts are structured and how you might use these in a real life example
- 21:36I definitely advise you to try this out on your own and try to create module scripts and
- 21:41maybe even just look at other module scripts that developers have created and if it looks
- 21:45really complicated I mean don't let yourself down on that because people do come a long way when it
- 21:50comes to developing and usually when you're the one writing it you understand your own code more
- 21:56than the developer that WR the code that you're looking at so that's something I wanted to show
- 22:00you before we ended off this episode I really hope you found this episode to be helpful and if you
- 22:05have then I would suggest liking the video and if you haven't you should join my Discord server
- 22:10the link is in the description and with that being said I will see you in the next episode take care
About this transcript
This page contains the full transcript of Module Scripts - Roblox Advanced Scripting #11 by BrawlDev, generated from the public captions YouTube serves with the video. The transcript has 4,095 words across 223 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.