Pathfinding npc glitches

I have an npc that goes to your position when you are in a clear line of sight but if you arent then it pathfinds to where are you, but when your in his line of sight and then go behind a wall or through a vent he stands still for a few seconds and starts to hop, but networkowner is set to nil

pathfinding function:

function pathfind(target)
	local agentParams = {
		["AgentHeight"] = 5,
		["AgentRadius"] = 2,
		["AgentCanJump"] = true,
		Costs = {
			Doors = 1
		}
	}
	local path = pathfindingService:CreatePath(agentParams)
	path:ComputeAsync(humanoidRootPart.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
				humanoid.Jump = true
			end
			humanoid:MoveTo(waypoint.Position)
			local waypointTime = humanoid.MoveToFinished:Wait()
			if not waypointTime then
				pathfind(target)
				break
			end
			if checkSight(target) then
				repeat
					humanoid:MoveTo(target.Position)
					wait()
					if target == nil or target.Parent.Humanoid.Health < 1 or humanoid.Health < 1 then
						break
					end
				until checkSight(target) == false
				break
			end
		end
	end
end