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

How to make a NPC keep its distance from Zombies?

Asked by 6 years ago

I need to make a NPC have its walkspeed set to 0 when it is close 20 studs or less to another NPC with its humanoid labeled "Zombie", so far I havn't managed to do it, if the NPC can walk away then it will be even better.

1 answer

Log in to vote
0
Answered by 6 years ago

Do you want the NPC to walk away from the Zombie or have its WalkSpeed set to 0? Because the NPC can't really move if it has 0 WalkSpeed, if you want to set the Walkspeed to 0 then its really easy.

wait(1)

print('Script Loaded')


local NormalSpeed = script.Parent.Humanoid.WalkSpeed
function FindChild(pos)
    local ListOfObj = workspace:GetChildren()
    local Dist = math.random(20, 40)
    local Found = nil
    local IsHuman = nil
    local HumanPart = nil
    local Torso = nil
    for x = 1, #ListOfObj do
        Found = ListOfObj[x]
        if (Found.ClassName == "Model") and (Found ~= script.Parent) then
            IsHuman = Found:FindFirstChild("Zombie")
            HumanPart = Found:FindFirstChild("Torso")
            if (HumanPart ~= nil) and (IsHuman ~= nil) and (IsHuman.Health ~= 0) then
                if (HumanPart.Position - pos).magnitude < Dist then
                    Torso = HumanPart
                    Dist = (HumanPart.Position - pos).magnitude
                end
            end
        end
    end
    return Torso
end

while true do
    wait()
    local Dir_Pos = FindChild(script.Parent.Torso.Position)
    if Dir_Pos ~= nil then
        print 'Zombie was found!'
        script.Parent.Humanoid.WalkSpeed = 0
    else
        script.Parent.Humanoid.WalkSpeed = NormalSpeed
    end
end

Ad

Answer this question