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

How can I make my zombie chasing script more effective?

Asked by 2 years ago
Edited 2 years ago

Hello, I tried making a script that makes a zombie npc follow the closest player to it and automatically update which player to follow. This is what I currently have (the script is inside of the zombie npc):

local PFS=game:GetService("PathfindingService")
local Zomb=script.Parent
local HRP=Zomb:WaitForChild("HumanoidRootPart")
local Humanoid=Zomb:WaitForChild("Humanoid")
local ClosestChar=nil
local ClosestDist=10000

--Find Closest Player
local function findClosestChar()
    ClosestChar=nil
    ClosestDist=10000
    for i,v in pairs(game.Players:GetChildren()) do
        if v.Character and v.Character.HumanoidRootPart then
            local PlayerHRP=v.Character.HumanoidRootPart
            if ClosestChar==nil or (PlayerHRP.Position-HRP.Position).magnitude<ClosestDist then
                ClosestChar=v.Character
                ClosestDist=(PlayerHRP.Position-HRP.Position).magnitude
            end
        end
    end
    if ClosestChar then
        return ClosestChar.HumanoidRootPart.Position
    end
end

--Go To Position
local function GoTo(End)
    local Path=PFS:CreatePath()
    Path:ComputeAsync(HRP.Position,End)
    local wayPoints=Path:getWaypoints()
    for i=1,5 do
        if wayPoints[i] then
            if wayPoints[i].Action==Enum.PathWaypointAction.Jump  then
                Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
            end
            Humanoid:MoveTo(wayPoints[i].Position)
            Humanoid.MoveToFinished:Wait(2)
        end
    end
end

while true do
    if findClosestChar() then
        GoTo(findClosestChar())
    else wait()
    end
end

The script works, however, the zombie will stutter on his way to his destination and I want him to walk smoothly the entire way there. I'm mainly looking to fix my 2nd function (GoTo), but I'm open to any suggestions on any part.

0
If anyone else has the same problem as me, the answer below only worked sometimes, I watched some more videos and they reccomend to use ray casting to detect once the zombie has a direct line to its target instead of using pathfinding service and this worked boqueburro 57 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

Humanoid.MoveToFinished:Wait(2)

Remove this line

Ad

Answer this question