NPC Pathfinding Script makes it "Twitch"

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to create a pathfinding script for an NPC. But as the title suggests, the NPC keeps “twitching” when following the player.
  2. What is the issue? Include screenshots / videos if possible!
    I am unable to provide a video at the moment. I will update this post if it is possible.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried increasing the distance required for the npc to find a new set of waypoints, but to no avail.

Here is my current code

local npc=script.Parent
local human=npc.Humanoid
local head=script.Parent:WaitForChild("Head")
hrp=nil
local service=game.PathfindingService
local lhrp=npc.HumanoidRootPart
local a=false
human.MoveToFinished:Connect(function()
    a=true
end)
local ts=game:GetService("TweenService")
local function walkto(target)
    if target then
        local path=service:CreatePath({AgentCanJump=true})
        path:ComputeAsync(lhrp.Position,target.Position)
        if path.Status~=Enum.PathStatus.Success then
            return
        end
        con=path.Blocked:Connect(function()
            con:Disconnect()
            return
        end)
        local points=path:GetWaypoints()
        for i,waypoint in points do
            if hrp then
                local stunned=hrp.Parent:FindFirstChild("IsStunned").Value
                if stunned==true then
                    break
                end
            end
            human:MoveTo(waypoint.Position,hrp)
            if waypoint.Action==Enum.PathWaypointAction.Jump then
                human:ChangeState(Enum.HumanoidStateType.Jumping)
            end
            repeat task.wait() until a or (target.Position-human.WalkToPoint).Magnitude>5
            a=false
        end
    end
end
local function getnearest()
    local nearest=nil
    for i,char in pairs(workspace:GetChildren()) do
        if char:IsA("Model") and char~=script.Parent then
            if char:FindFirstChildWhichIsA("Humanoid") and game.Players:GetPlayerFromCharacter(char) and char:FindFirstChild("IsStunned").Value~=true and service:FindPathAsync(lhrp.Position,char.PrimaryPart.Position).Status==Enum.PathStatus.Success then
                hrp=char.PrimaryPart
                if nearest then
                    if (hrp.Position-head.Position).Magnitude<(nearest.Position-head.Position).Magnitude then
                        nearest=hrp
                    end
                else
                    nearest=hrp
                end
            end
        end
    end
    return nearest
end
while task.wait() do
    local last
    hrp = getnearest()
    if hrp then
        last=hrp
        walkto(hrp)
    end
end

I apologize if the post or the code is structured incorrectly. It is my first time asking for help here.

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

4 Likes