Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

NPC Script Shortening with Modules?

Asked by 6 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?

wait (3)
NPC = script.Parent
Children = NPC:GetChildren()
humanoid = NPC.Humanoid
IsHurt = false
SpawnPos = NPC.Spawn.Position
humanoid.Health = humanoid.Health - 5
print ("An NPC named: " ..NPC.Name.. " has been born")

function ContinueWalking()
    NPC.Busy.Value = false  
    while NPC.Busy.Value == false do
        wait (math.random(4, 6))
        humanoid:MoveTo(Vector3.new(SpawnPos.X, 7.4, SpawnPos.Z) + Vector3.new(math.random(-90, 90), 0, math.random(-90, 90)), NPC.Spawn)
        wait (math.random(3, 6))
        humanoid:MoveTo(Vector3.new(SpawnPos.X, 7.4, SpawnPos.Z) + Vector3.new(math.random(-90, 90), 0, math.random(-90, 90)), NPC.Spawn)
    end
end

function StopWalking()
    NPC.Busy.Value = true
end

function RunFromDanger()
    print (NPC.Name.. " is in danger!")
    NPC.Busy.Value = true
    IsHurt = true
    walkspeed = humanoid.WalkSpeed

    for i,v in pairs(Children) do
        if v.className == "Tool" then
            v:Destroy()
        end
    end

    repeat
        humanoid.WalkSpeed = 25
        wait (.5)
        humanoid:MoveTo(Vector3.new(SpawnPos.X, 7.4, SpawnPos.Z) + Vector3.new(math.random(-50, 50), 0, math.random(-50, 50)), NPC.Spawn)
        wait (.5)
        humanoid:MoveTo(Vector3.new(SpawnPos.X, 7.4, SpawnPos.Z) + Vector3.new(math.random(-50, 50), 0, math.random(-50, 50)), NPC.Spawn)
    until humanoid.Health == humanoid.MaxHealth
    humanoid.WalkSpeed = walkspeed
end

function ReturnToSpawn()
    IsHurt = false
    print (NPC.Name.. " is returning to spawn")
    humanoid:MoveTo(Vector3.new(SpawnPos.X, 7.4, SpawnPos.Z), NPC.Spawn)
    NPC.Busy.Value = false
end


function Hurt()
    NPC.Busy.Value = true
    if humanoid.Health > 0 and humanoid.Health < humanoid.MaxHealth and NPC.Busy.Value == true and IsHurt == false then
        StopWalking()

        RunFromDanger()

        ReturnToSpawn()

        ContinueWalking()
    end
end

humanoid.HealthChanged:connect(Hurt)

1 answer

Log in to vote
0
Answered by
MunimR 125
6 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)

--functions and variables here

shared["lib"]=getfenv()
local function Loader()
    for i,v in pairs(shared["lib]) do
        if i~="script" then
            getfenv(2)[i]=v
        end
    end
end

return Loader

So to require it all you would have to do is

require(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