Hello, I'm working on my NPC for my game and so far have achieved awesome progress it works flawlessly as of so far however, to make it easier for my self i'd love to get into Module scripts as that will allow me to easily navigate through my code when it comes to error fixing and adding code in general. The issue is, where do i get started? I understand the practicality of module scripts but when i open up one of those scripts i feel really awkward on how to use them.
Here is all my code for the NPC for those who are interested:
-Controller
01 | local Event = script.Parent:WaitForChild( "findTarget" ) |
02 |
03 | local npcStorage = workspace.Game.npcStorage |
04 |
05 | while true do wait( 1 ) |
06 | if #npcStorage:GetChildren() > 0 then |
07 | Event:FireServer() |
08 | else |
09 | script.Parent:WaitForChild( "Target" ).Value = "" |
10 | end |
11 | end |
-TargetFinder
01 | local Event = script.Parent:WaitForChild( "findTarget" ) |
02 |
03 | Event.OnServerEvent:Connect( function () |
04 | local NPC = script.Parent.Parent |
05 | local myTeam = NPC.AI:WaitForChild( "Team" ) |
06 | local HRP = NPC:WaitForChild( "HumanoidRootPart" ) |
07 |
08 |
09 | local Target = NPC.AI:WaitForChild( "Target" ) |
10 | local NPCs = workspace.Game.npcStorage |
11 |
12 |
13 | local closestTarget = nil |
14 | local targetsDistance = nil |
15 |
-Movement
01 | local atkDistance = 3.5 |
02 | local Tower = workspace.Game:FindFirstChild( "RedTower" ) |
03 |
04 |
05 |
06 | --/// myNPC |
07 | local NPC = script.Parent.Parent |
08 | local HRP = NPC:WaitForChild( "HumanoidRootPart" ) |
09 | local Hum = NPC:WaitForChild( "Humanoid" ) |
10 |
11 | local AI = NPC:WaitForChild( "AI" ) |
12 | local Team = AI:WaitForChild( "Team" ) |
13 | local Target = AI:WaitForChild( "Target" ) |
14 | local moveToTower = AI:WaitForChild( "moveToTower" ) |
15 |
If you need to know any more information please let me know. And thankyou if you can explain how to use Module Scripts.
ModuleScript
objects have default code already pasted, similar to Script
s and LocalScript
s how they have print("Hello world")
as the default code. This is the default code for modules:1 | local module = { } |
2 |
3 | return module |
1 | local module = { } -- You can name them anything, I just used the default name |
2 |
3 | function module.functionInModule() |
4 | print ( "I’m a function inside a module!" ) |
5 | end |
6 |
7 |
8 |
9 | return module -- required to work |
local
before function
. local function
1 | local module = require(game.Workspace.ModuleScript) -- i put in workspace |
2 |
3 | module.functionInModule() -- prints code |
ModuleScript
s is that they take on the behaviour of a Script
or a LocalScript
, depending on what you use. For example, if you used LocalPlayer
in a module, you MUST require
it from a LocalScript
else the script will error and LocalPlayer
will return nil
, since LocalPlayer
is client-side only.Hello there! I am excited that you are giving ModuleScripts
a chance for the sake of organization.
Although there are good practices and bad practices many people use modules to a different extent, because as is with a normal script, it heavily depends on your coding style and how much code you can handle in your regular scripts before you consider it unorganized.
With that in mind, first I will teach you the basic functionality of a module, and then I will show you how I use modules.
--
A module is like any other script, the code in it runs from top to bottom, and it only runs once.
If you have a loop inside it, that loop will obviously still loop, but a ModuleScript is not a function that you call.
The primary differences are as follows:
required
return exactly one value
Module Script
1 | print ( "ModuleScript is running" ) |
2 |
3 | return true |
"Normal" (Server) Script
1 | local testing = require(script.Testing) --> true |
2 |
3 | if testing then |
4 | print ( "TEST MODE ACTIVATED!" ) |
5 | else |
6 | print ( "THIS IS NOT A DRILL!" ) |
7 | end |
In this example, all the module does is print that it is running, and then returns true
. In this case, I am letting the script that is requiring it know that the game is currently in "test mode", whatever that means.
With that being said, you can require the same module in multiple scripts, and then they will all know that the game is in test mode. If you want to change the test mode, you just have to change it in the module, and not have to worry about each individual script.
Module Script
01 | local quickmaths = { } |
02 |
03 | quickmaths.Add = function (a, b) |
04 | if a and b then |
05 | local res = a + b |
06 | print (( "%d + %d = %d" ):format(a, b, res)) |
07 | return res |
08 | else |
09 | print ( "2 + 2 = 4" ) |
10 | return 2 + 2 |
11 | end |
12 | end |
13 |
14 | quickmaths.Subtract = function (a, b) |
15 | if a and b then |
"Normal" (Server) Script
1 | local quickmaths = require(script.quickmaths) |
2 |
3 | local seven = quickmaths.Add( 5 , 2 ) |
4 | local four = quickmaths.Subtract(seven, 3 ) |
5 |
6 | quickmaths.Add() |
7 | quickmaths.Subtract() |
ModuleScripts
are extensions of normal scripts. You can throw the clutter in them. Functions, tables, math, libraries. What does that leave your normal script looking like?
If you've done it right, at the end of it all, it is short, readable, and easy to understand the flow of the game.
--
Final notes:
Contact me on Discord if you have any questions, or would like more information about modules that I have not provided in this answer. My Discord tag is amanda#9999.
01 | local Object = { } |
02 |
03 | --------------- Function.Waves(BrickColor.new("Cyan"), Torso, 1, 0.5) |
04 |
05 | local Effects = game:GetService( "ServerStorage" ):WaitForChild( "Skills" ) |
06 | local Meshes = Effects:WaitForChild( "Assets" ) |
07 | local Parts = workspace:WaitForChild( "PartStorage" ) |
08 | local rep = game:GetService( "ReplicatedStorage" ) |
09 | local Assets = rep:WaitForChild( "Assets" ) |
10 | local auras = rep:WaitForChild( "Aura" ) |
11 | local auras 2 = rep:WaitForChild( "WideAuras" ) |
12 |
13 | ----------- DUST 2.0 |
14 | function Object.Dust(root, duration) |
15 | local dust = Assets.Dust:Clone() |