So I am scripting a game and am wondering what the benefits of module scripts are. I don't think that it is possible(I might be wrong) to pass arguments to functions inside of module scripts. So I am really confused as to the benefits. If someone could clarify this I would really be appreciative.
Well, Module Scripts is another type of Script that is supported by the Roblox Engine. It does all the features a script needs to do. However, the main difference is that the script does not run in the beginning of the game. The only way it can run is by calling it in another script. Basically, the module script itself acts as a function!
When you are writing same repetitive code for different tasks, it is more efficient to write on one spot and then calling it into another scripts. This really saves you a lot of time when used wisely. It also allows you to maintain and update you code easier as you can just change it into one spot rather than changing every single code. It can also be used to store information and frameworks which has to be accessed by both client
and server
Let's break down even more.
--Lets say you want to call a module script which prints the word "Hello World" --ServerScript require(game.Workspace:WaitForChild("PrintingHelloWorldModule"))
--Module Script Named PrintingHelloWorldModule print ("Hello World!")
What do you notice? You get an error! Why? Because you are not returning the value!
Error Message: 22:45:04.543 - Module code did not return exactly one value 22:45:04.544 - Requested module experienced an error while loading
The Fix
--ServerScript] require(game.Workspace:WaitForChild("PrintingHelloWorldModule"))
--Module Script Named PrintingHelloWorldModule print ("Hello World!") return true
Now, did you notice that when you first create a module script, you see the following code?
local module = {} return module
Hmmm, what could that mean? Why is it inserting a table? We know what the return does and its importance. But why do we need the module = {}'???
Well the answer is because: Module Scripts only return single value.
Take a look at the example below.
--ServerScript] require(game.Workspace:WaitForChild("PrintingHelloWorldModule"))
--Module Script Named PrintingHelloWorldModule local num1, num2, num3 = 2, 4, 6 return num1, num2, num3
What do you notice? You get the same error! Why? Because you cannot return more than one value! However, do keep in mind. Module Scripts can return anything! The only thing that does not work is just simple return
I don't think that it is possible to pass arguments to functions inside of module scripts.
--Module Script local function printValues (Value) print(Value) end return printValues
--ServerScript local moduleScript = require(game.Workspace:WaitForChild("ModuleScript")) moduleScript("Hello!")
how would I use a module script for a touched event?
What we will do with this. We will change a part's colour when you touch the detecting part
--Module Script -- Processing Section local function doWorkOnTouch (hit) local Workspace = game:GetService("Workspace") local Part = Workspace:FindFirstChild("Part") Part.BrickColor = BrickColor.Random() print(Part.BrickColor) end -- Connecting Section return doWorkOnTouch
--Server Script local moduleScript = require(game.Workspace:WaitForChild("ModuleScript")) repeat wait() until game.Players.LocalPlayer script.Parent.Touched:Connect(function(hit) local Character = hit.Parent local Humanoid = Character:FindFirstChild("Humanoid") if Humanoid then moduleScript() end end)
And thats how you do it!