NPC jumps back and forth when walking off a big enough part

There’s a problem with the NPC and it’s that, instead of jumping or walking off a big enough part, it just walks and jitters back and forth like it doesn’t know what to do.

function NPC:findPath(beginningPosition, targetPosition)
	local path = pathfindingService:CreatePath({
		agentRadius = 2;
		agentHeight = 5;
		agentCanJump = true;
		agentCanClimb = true;
		waypointSpacing = 1;
		costs = {};
	})

	local success, errorMessage = pcall(function()
		path:ComputeAsync(beginningPosition, targetPosition)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()
		
		pathBlockedConnection = path.Blocked:Connect(function(blockedIndex)
			if blockedIndex >= nextIndex then
				pathBlockedConnection:Disconnect()
				NPC:findPath(beginningPosition, targetPosition)
			end
		end)

		for i, waypoint in pairs(waypoints) do
			if not reachedPointConnection then
				reachedPointConnection = humanoid.MoveToFinished:Connect(function(reached)
					if reached and nextIndex < #waypoints then
						nextIndex += 1
						humanoid:MoveTo(waypoints[nextIndex].Position)
						if waypoint.Action == Enum.PathWaypointAction.Jump then
							humanoid.Jump = true
						end
					else
						reachedPointConnection:Disconnect()
						pathBlockedConnection:Disconnect()
					end
				end)
			end
		end

		nextIndex = 2
		humanoid:MoveTo(waypoints[nextIndex].Position) 
	else
		warn(errorMessage)
	end
end

Here’s the video:
robloxapp-20240928-1908403.wmv (1.7 MB)

Keep in mind this is my first post.
I couldn’t find any topics that talked about this. I’ve spent hours on this, it’s frustrating.

Nevermind. I coded a simpler findPath system I originally didn’t like, as it stuttered. But it walked off of ledges, so I pathfinded whenever a ray (casted from the NPC’s HumanoidRootPart to the target) was blocked.