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

NPC Script Shortening with Modules?

Asked by 11 years ago

I just wanted to know if it would be possible if I could put these functions into one module script, or would I have to put each function into a seperate Module Script?

01wait (3)
02NPC = script.Parent
03Children = NPC:GetChildren()
04humanoid = NPC.Humanoid
05IsHurt = false
06SpawnPos = NPC.Spawn.Position
07humanoid.Health = humanoid.Health - 5
08print ("An NPC named: " ..NPC.Name.. " has been born")
09 
10function ContinueWalking()
11    NPC.Busy.Value = false 
12    while NPC.Busy.Value == false do
13        wait (math.random(4, 6))
14        humanoid:MoveTo(Vector3.new(SpawnPos.X, 7.4, SpawnPos.Z) + Vector3.new(math.random(-90, 90), 0, math.random(-90, 90)), NPC.Spawn)
15        wait (math.random(3, 6))
View all 67 lines...

1 answer

Log in to vote
0
Answered by
MunimR 125
11 years ago

Depends honestly, ModuleScripts aren't needed they are just a tool for organization, and that means you can organize them to your liking, organize the functions by what should call them or organize functions by how or when they are called. Honestly for these functions I would first add parameters so you can actually use the code in ModuleScripts, and then after that I would put them all into one ModuleScript with a loading functions which loads all the variables in the current ModuleScript.

something like this in the ModuleScript. (under the workspace)

01--functions and variables here
02 
03shared["lib"]=getfenv()
04local function Loader()
05    for i,v in pairs(shared["lib]) do
06        if i~="script" then
07            getfenv(2)[i]=v
08        end
09    end
10end
11 
12return Loader

So to require it all you would have to do is

1require(Workspace.ModuleScript)()

As I said there are a billion ways to organize modulescripts, there is no right or wrong way its all your choice of organization. If I am not clear enough please comment and I'll willingly explain more :)

Ad

Answer this question