Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

What is the best way for me to use moule scripts in my game?

Asked by 6 years ago

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.

0
Module scripts are a secure way to load a script into a game. Essentially, you have a module with all the fancy script stuff, and then you have a command which runs the module. tek_o 56 — 6y
0
Oh. so how would I use a module script for a touched even? Thx so much for the tip though. User#21908 42 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

Hello there! I will be teaching you the basics of Module Scripts and its uses in a game.

What are Module Scripts?

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!

Why is it useful?

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

I think we covered the basics of Module Script. Let's talk about how exactly to use them!

Now, Module Scripts have certain criteria

  • You need to return a value
  • Also, you can only return a single value.

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

Now lets go deeper in to your questions.

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!

Have a lovely day of coding!
0
That was the most beautiful answer I have ever seen on a Q and A website. Faazo 84 — 6y
0
Thank you so so much. User#21908 42 — 6y
Ad

Answer this question