What is a practical use for module scripts?

I find many other people using module scripts in their games but I don’t quite see how to use them.

I feel like I might be able to ease a lot of parts in my scripts so I was just wondering, how do you use module scripts practically?

2 Likes

A simple one is having the same function accessible by multiple scripts.
Let’s say you have

local function Add(a: number, b: number)
    return a + b
end

It would be annoying to copy and paste this everywhere and if you wanted to change its behaviour, you’d need to do this every single place that you’d use this function.

Modulescripts fix this.

I could also go into ideas about OOP, but much better posts have been done so I won’t go into them here.

4 Likes

Here’s a list that is not all inclusive:

  • Reuse of certain code such as functions
  • Class creation with metatables
  • Storing data for cross-script communication
  • Global variables
5 Likes

Could you give me an example or reference post on your third point: ‘Storing data for cross-script communication’?

That is something that would be very beneficial for me

2 Likes

modules are great for one thing…reusability. if i have a function that I need to use everywhere in the entire game I can just make one module and use that code anywhere.

2 Likes

MainModules Can be used not only for functions, but to load data which you would want to access by other scripts, as for example translations for different languages with dictionaries, or just to hold variables which are hard-coded and need to access by other scripts (somewhat proper alternative to _G and shared).

Their only limitations are the fact that you have to return table at the end of the script, but other than that you can think of them as one massive function, which can, for example return functions.
They are also pretty essential for OOP coding for libraries.

The simpliest form of mainmodule structure is:

return {}

and that is all that is needed for MainModule to be functional.
You can use it for translations as:

return {
   ["en-us"] = {
      ["CantDoThat"] = "I can't do that right now".
   },
   -- //Insert other translations
}

And then you could just require your translation by:

local translatedStrings = require(script.TranslationsModule)["en-us"] -- //Example

print(translatedStrings.CantDoThat)

Same could be done with functions as:

local genericTable = {}

function genericTable:Foo(bar) 
   -- //Could be also done as function genericTable.Foo or genericTable.Foo = function(bar)
   print(bar) 
end

return genericTable

And then the function could be used as:

local genericLib = require(script.GenericLibrary)

genericLib:Foo("Testing")

I hope you get the point!

3 Likes

Sure!

-- ModuleScript
local GLOBAL_VARS = {
  "VAR_1" = 1,
  "VAR_2" = 2,
  ...
}

return GLOBAL_VARS
-- ServerScript
local GLOBAL_VARS = require(ModuleScript)
print(GLOBAL_VARS["VAR_1"]) -- Output: 1

For cross-server data handling:

-- ModuleScript
local GLOBAL_DATA = {}
local temp = {}

function GLOBAL_DATA.add(key, value)
  temp[key] = value
end

function GLOBAL_DATA.get()
  return temp
end

return GLOBAL_DATA
-- ServerScript

local GLOBAL_DATA = require(ModuleScript)
GLOBAL_DATA.add("foo", "bar")
print(GLOBAL_DATA.get()["foo"]) -- Output: "bar"

That’s the main gist - consider this psuedocode, as it may not be 100% correct

3 Likes

Simply put, module scripts are scripts that provide code samples to other scripts. What this code is can be many things, such as variables and functions. It can even get into more complex things such as Object Oriented Programming (OOP, which I’m still trying to learn!).

1 Like

My most favourite use of module scripts is that when put in ReplicatedStorage, they can be accessed by both global and local scripts.

In addition, I use module scripts for general ordering of a complicated set of code.
Take y3llow mustang (kangerujack)'s AI helicopter for an example.


He uses multiple labelled module scripts which define functions that pertain to the module’s name. He then uses those functions in his main script, making it much shorter and easier to analyse.

1 Like

In my module BannerUI, I use ModuleScripts for UI effects such as hover effects, creating popup windows, changing screens, etc.

Module scripts in game development are powerful tools for organizing and reusing code. You can create functions and variables in a module script and then use them across multiple scripts or parts of your game.

Create a Module Script:

  • In your game, create a ModuleScript instance.
  • Open the script and define functions or variables inside it.

Require the Module Script:

  • In other scripts where you want to use the module’s functionality, use require to access it.
local myModule = require(game.ReplicatedStorage.MyModule) -- Adjust path accordingly

Use Functions/Vars from the Module:

  • Call functions or use variables defined in the module.
myModule.myFunction() -- Replace with your function name
local value = myModule.myVariable -- Replace with your variable name

Update ModuleScript:

  • If you make changes to the module, the changes are reflected everywhere it’s required.

Using module scripts helps keep your code modular, making it easier to manage and update. For example, if you have a set of utility functions, you can place them in a module and reuse them across different parts of your game.

You can also make module scripts accessible from both the server and client in Roblox by placing the module script in a location that both the server and client have access to. Typically, this is done by placing the module script in a location like ServerScriptService for server-side access and ReplicatedStorage for both server and client access.

1 Like

I apologize for a late reply but I think I see the use. You can add to a table that resides in a module script from script 1 and pull that value from script 2. Thank you for a great example.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.