Understanding Module Scripts

Hello, i currently(Much time before.) found myself in an issue on not understanding Module Scripts.

I literally cannot understand these at all, i used those articles:

All The Type of Scripts!
Not actually an article?: Module Script

If someone explains me, and at least i learn something, i will be greatly thankful.

So I may not be the best to add on to this topic, but I myself have recently been getting into it, so here we go.

A module script enables you to access functions without cluttering the current script you have. for example,

--MODULE SCRIPT
local UniversalFunctions = {}

function UniversalFunctions.one()
  print("hey hey hey!")
end

What the module script has done is appended the previous local function table to include the function of .one, now to follow on in a script,

--Script
local modulescriptfunctions = require(script.Parent.Modulescript)

modulescriptfunctions.one()

What this script now does is it gets access to the module script function, thus decluttering your script.

Hope this helps!

1 Like

Module scripts are a way to apply to the DRY principle (Don’t-Repeat-Yourself). Think of them as a way of calling the same function over and over across all your scripts.

A module script is simply a code container that allows you to re-use code without having to paste the same chunk of code everywhere. This allows you to make changes all in once place without having to change once mistake in 30 other scripts.

1 Like

I have been getting this error while testing module scripts:

17:16:11.923 - Module code did not return exactly one value

Code:

local Functions = {}

function Functions.GetChildrens()
	for _, Part in pairs(workspace:GetChildren()) do
		if Part:IsA("BasePart") then
			return Part
		end
	end
end

And an other error from the/a ServerScript:

17:16:11.926 - Requested module experienced an error while loading

local Module = require(game:GetService("ReplicatedStorage"):WaitForChild("ModuleScript"))

Module.GetChildrens()

Try returning “Functions” at the end of the script. so

local Functions = {}

function Functions.GetChildrens()
	for _, Part in pairs(workspace:GetChildren()) do
		if Part:IsA("BasePart") then
			return Part
		end
	end
end
return Functions

After reading the edited version I can see few mistakes there, I can reccomend along with the changes I made in my previous comment, you also change the code so you do not do :getservice direct within the require(). Just a personal preference.

local Module = require(game.ReplicatedStorage.ModuleScript)

modulescriptfunctions.GetChildrens
1 Like

After using the return Functions, both errors got solved, also, i just used GetService because it sounds more “unique”, and i thought you need to use WaitForChild while indexing childs of ReplicatedStorage.

Great to hear! I reccomend indicating a solution to people understand the issue has been resolved and do not reply when the errors are fixed. Have a great rest of your day :slight_smile:

2 Likes