What uses do module scripts possess

I’m have trouble using module scripts inside the game itself, I cant really find a good use for them, how can I apply a module script to a in game function?

Create a ModuleScript:

local module = {}

function module.function()
-- do something
end

return module

Using a ModuleScript:

require(ModuleScript).function()

I wouldn’t use them, they are usually just scams and it’s better to only use LocalScripts

2 Likes

They can be useful if you like to stay organized with your functions, but I just jumble them and duplicate them personally lol

1 Like

A module script is a great way to create a re-useable script asset for your game. A good example is making weapons for an FPS, you would use a main script for setting the unique properties of each gun and then use a module to handle the core functions of the weapons.

4 Likes

modulescripts are good for making your own functions in your game. Simple to use but difficult to learn in my opinion.

I use modulescripts whenever a feature of an existing script is seperable, as in it could have been used by a different script for the same purpose. Here are what the module scripts in my placement GUI are:

image

It is difficult to design a modulescript from scratch. I almost always end up with them by writing everything in one script and then moving things to modules as the “good places to split off” become more obvious. The process is similar in other types of programming.

2 Likes

You can use module scripts for wave systems like in tower defense games or gun systems.

ModuleScripts can be used for OOP, the use for OOP is to create multi uses with one ModuleScript. Another way is just making a ModuleScript for a mechanic in your script so its not like 1000 lines long like with Roblox CoreScripts which uses alot of modules for mechanics of the RobloxGui.

Module scripts are incredibly useful for any given data storage use. Let’s say you want a health system that doesn’t rely on humanoid. You’d set up a modulescript like this:

local module = {}

module.Health = 100
module.MaxHealth = 100

return module

and to access it, you use require()

local OurModule = require(script.Parent.ModuleScript) --you only need to require once you've declared it.
print(OurModule.Health) --100
OurModule.Health -= 20
print(OurModule.Health) --80

You can also store functions in them as well, so you won’t have to copy and paste that same function everywhere.

local module = {}

module.Health = 100
module.MaxHealth = 100
module.Armor = 0.5 --50%

function module.TakeDamage(Damage,AP)
   if Damage ~= 0 then 
       AP = AP or 0 --If AP is nil, set to 0.
       PenetratedArmor = 1 - (Armor - AP) --Yes this can result in >1 PA if the AP is higher than defense but who cares.
      module.Health -= Damage * PenetratedArmor
   end
end

return module

and to access functions from a module script, do this

local OurModule = require(script.Parent.ModuleScript) 
print(OurModule.Health) --100
OurModule.TakeDamage(50,0)
print(OurModule.Health) --75
8 Likes

Amazing. Every word of what you just said was wrong!

5 Likes

ModuleScripts are a Script that Stores Data. They are Treated as a way to store Specific data for specific tasks.

Instead of Hello world!, You will see: local module = {} return module, Don’t get confused for worried about that

Let me explain:
local module = {} Is a Table, This is used to store Items like functions, Variables, or Events

return module returns all the Assets within that Table, so when getting its contents via require(), you can access it


This would be one way of Storing data in the Module

local module = {

   A = "Hello";
   B = 1e2; -- 100
   CopyOfA = A; -- Not Valid
   
   Func = function()  -- function

   end)

}

However, This is Ugly, Plus, you aren’t able to access those Variables, The more Proper way would be:

local module = {}

module.A = "Hello"
module.B = 1e2
module.CopyOfA = module.A -- Valid

function module.Func() -- A Function
end

module.Func2 = function() -- Also A Function

end

This is Better for being more Organized


Normally when Assigning a Variable to Something, you do this:

local Thing = workspace.Thing -- A Thing!

But with ModuleScripts, You have to use require() to access its contents, otherwise you only have a Instance rather than a Portal for code

local Thing = require(workspace.Thing) -- Accesses Module Content

So when you want to use something, Its as simple as this:

local Thing = require(workspace.Thing) -- Accesses Module Content

print(Thing.A) -- Should print "Hello"

You may have heard of the word self thrown around in ModuleScripts,

self alone is just a Variable, nothing more,
It is used for something called: OOP (Object Oriented Programming),
self is only able to be used (I believe) with a Colon : function:

function module.Foo() -- Cannot use "self"
   print(self) -- Underlined red
end

function module:Bar() -- Can use "self"
   print(self) -- predetermined
end

Here is one Example of self:

function module:Bar()
   local self = {} -- Self is Assigned as a Table
   self.Text = "Hello" -- New Text Variable
   self.Number = 12345 -- New Number Variable
   self.Bool = false -- New Boolean Variable

   return self -- returns new Data
end

Treat them like Regular Scripts, Call them when you need them and Use them when necessary,
They are good way to not Repeat yourself in coding, and for making your code Cleaner and Organized

Remember to Protect your ModuleScripts, They can be decompiled

I recommend putting the Important ones inside ServerScriptService or ServerStorage so they are only accessed by the Server so that way, hackers will have a harder time accessing them, for both Client and Server use, ReplicatedStorage


Also, Don’t Listen to @twinqle , that’s just flat out wrong


5 Likes

Ignore him, he’s just a troll account.

2 Likes

A module script in Roblox is a particular kind of script that defines reusable variables, functions, and other values that can be used in different areas of your game. To organize your code and make it simpler to reuse and maintain, use a module script.

Then, you may utilize the module script’s functions and values in your script in the same way that you would any other local variables or functions.

You can use the require function to import the module script from another script in order to utilise the functions and values defined within.

Remember that a module script’s defined values are by default private, meaning that only the module script itself or scripts that depend on the module script can access them. You can return values as a part of a table at the conclusion of the module script to make them visible to the public and reachable from outside the module script.

The aforementioned syntax can then be used to access the public variables and functions from another script.

I decided it would be fun to give this answer some thinking.

2 Likes

dude I was having problems with humanoid health, never thought of doing health with script module thanks :))

2 Likes

?

They aren’t, ModuleScripts are very useful for Functions that you’ll need more than once from different scripts, so you don’t write it 100 different times from 100 different scripts

2 Likes