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
local Event = script.Parent:WaitForChild("findTarget")
local npcStorage = workspace.Game.npcStorage
while true do wait(1)
if #npcStorage:GetChildren() >0 then
Event:FireServer()
else
script.Parent:WaitForChild("Target").Value = ""
end
end
-TargetFinder
local Event = script.Parent:WaitForChild("findTarget")
Event.OnServerEvent:Connect(function()
local NPC = script.Parent.Parent
local myTeam = NPC.AI:WaitForChild("Team")
local HRP = NPC:WaitForChild("HumanoidRootPart")
local Target = NPC.AI:WaitForChild("Target")
local NPCs = workspace.Game.npcStorage
local closestTarget = nil
local targetsDistance = nil
for _, NPC in pairs(NPCs:GetChildren()) do
local eHRP = NPC:FindFirstChild("HumanoidRootPart")
for _, child in pairs(NPC:GetChildren()) do
if child.Name == "AI" and child:FindFirstChild("Team").Value ~= myTeam.Value then --and (HRP.Position - eHRP.Position).magnitude < targetsDistance then
local Team = child:FindFirstChild("Team")
closestTarget = NPC.Name
targetsDistance = (HRP.Position - eHRP.Position).magnitude
Target.Value = closestTarget
end
end
end
end)
-Movement
local atkDistance = 3.5
local Tower = workspace.Game:FindFirstChild("RedTower")
--/// myNPC
local NPC = script.Parent.Parent
local HRP = NPC:WaitForChild("HumanoidRootPart")
local Hum = NPC:WaitForChild("Humanoid")
local AI = NPC:WaitForChild("AI")
local Team = AI:WaitForChild("Team")
local Target = AI:WaitForChild("Target")
local moveToTower = AI:WaitForChild("moveToTower")
--/// Event
local Event = script.Parent:WaitForChild("Trigger")
local findTarget = script.Parent:WaitForChild("findTarget")
Hum:MoveTo(Tower.Target.Position)
Target.Changed:Connect(function()
--/// Enemy NPC
local eNPC = workspace.Game.npcStorage:FindFirstChild(Target.Value)
local eHRP = eNPC:FindFirstChild("HumanoidRootPart")
while true do wait(.1)
if Target.Value == "" or eNPC:FindFirstChild("Humanoid").Health <= 0 then
Target.Value = ""
Hum:MoveTo(Tower.Target.Position)
--/// Attack or Move
elseif (HRP.Position - eHRP.Position).magnitude <atkDistance then -- Checks if the distance between the enemy and NPC is less than 5 studs
Event:FireServer()
elseif Target.Value ~= nil then
Hum:MoveTo(eHRP.Position)
--Hum.MoveToFinished:Wait() -- May improve srv performance but will make NPCs appear derpy as they may walk to far than suppose to
end
end
end)
If you need to know any more information please let me know. And thankyou if you can explain how to use Module Scripts.

3 comments
Modules are like bindable functions. They can be passed using seperate scripts. To get a module script's and all of it's function, use "require(moduleplacing)". I'd show you mine but i'm new to scripting helpers and idk how to post scripts. Anyways, you give the module a name, "local Object = {}" then a function inside like "function Object.NAME()" then call it from another script.
You call it using the variable name that requires the module, like "Object = require(module)". Then, to call a function, do "Object.FunctionName(arguments)". Modules are very simple.
You really shouldn't have the client telling the server when npcStorage has stuff.