Pathfinding NPC Stutters

I’m trying to make pathfinding zombies. The problem, however, is the zombies’ walk animations will stutter until they are close enough to the target they are pursuing. I have no clue why this is happening. If anyone is experienced with pathfinding or has had the same issue, please help; this issue is ruining the cool animations I made.

2 Likes

Can you show me the script that you use to pathfind?

1 Like

Sure, I think this is the function that makes it stutter:

	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(myRoot.Position,target.Position)
	local waypoints = path:GetWaypoints()
	
	if path.Status == Enum.PathStatus.Success then
		for _, waypoint in ipairs(waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				myHuman.Jump = true
			end
			myHuman:MoveTo(waypoint.Position)
			local timeOut = myHuman.MoveToFinished:Wait(1)
			if not timeOut then
				myHuman.Jump = true
				print("Path too long!")
				findPath(target)
				break
			end
			if checkSight(target) then
				repeat
					print("Moving directly to the target")
					myHuman:MoveTo(target.Position)
					attack(target)
					wait(0.1)
					if target == nil then
						break
					elseif target.Parent == nil then
						break
					end
				until checkSight(target) == false or myHuman.Health < 1 or target.Parent:FindFirstChildWhichIsA("Humanoid").Health < 1
				break
			end
			if (myRoot.Position - waypoints[1].Position).magnitude >  20 then
				print("Target has moved, generating new path")
				findPath(target)
				break
			end
		end
	end
end
1 Like

actually I just fixed it by deleting

local timeOut = myHuman.MoveToFinished:Wait(1)
			if not timeOut then
				myHuman.Jump = true
				print("Path too long!")
				findPath(target)
				break
			end
1 Like

You can try using this, but I’m not sure if it will work, since I haven’t been able to test it:

local function moveToSmoothly(humanoid, targetPosition)
    local startPosition = humanoid.Parent.Position
    local distance = (startPosition - targetPosition).magnitude
    local duration = distance / humanoid.WalkSpeed
    humanoid:MoveTo(targetPosition)
    wait(duration)
end

local function followPath(target, myRoot, myHuman)
    local path = game:GetService("PathfindingService"):CreatePath()
    path:ComputeAsync(myRoot.Position, target.Position)
    local waypoints = path:GetWaypoints()
    
    if path.Status == Enum.PathStatus.Success then
        for _, waypoint in ipairs(waypoints) do
            if waypoint.Action == Enum.PathWaypointAction.Jump then
                myHuman.Jump = true
            end
            moveToSmoothly(myHuman, waypoint.Position)
            local timeOut = myHuman.MoveToFinished:Wait(1)
            if not timeOut then
                myHuman.Jump = true
                print("Path too long!")
                findPath(target)
                break
            end
        end
    end
end

I’m assuming you have variables for things such as myHuman and target outside of the part of the scriopt you gave me.

2 Likes

thanks for the reply, but the original script works fine without the timeout thing which uses MoveToFinished:Wait(1). I appreciate you taking time to try to help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.