Help with pathfinding AI bug

I’m trying to fix me and my friends Pathfinding ai, the problem is…

The function where it tells the AI that it cannot see or find any nearby player, forces the ai to stop pathfinding and go directly towards the player or dummy. Without the NoPathMove, the ai is really good. With it, the ai will stutter back and forth when jumping, and won’t go over ledges. The only problem is that we need that line of code. is there anyway we can fix this?

If so, much appreciated!


function NoPathMoveTo(ClosestModel) -- If there is nothing in the way, this makes it not stutter
	Humanoid:MoveTo(ClosestModel)
	if Settings.DebugWithPoints == true then
		Killer.DebugStorage:ClearAllChildren()
	end
end

RunService.Heartbeat:Connect(function()
	task.wait(0.1)
	local DistanceTable = FindAllDistances()
	if DistanceTable then
		Chasing = true

		local ClosestModel = DistanceTable[1][1]
		script.ClosestModel.Value = ClosestModel
		if CanSeeTarget(ClosestModel) then
			NoPathMoveTo(ClosestModel.HumanoidRootPart.Position)
		else
			update(script.ClosestModel.Value)
		end
		-- Attacking
		local Mag = (RootPart.Position - ClosestModel.Torso.Position).Magnitude
		if Mag < 0 then
			Mag = math.abs(Mag)
		end

		--[[if Settings.FindDistance == true then
			if Mag < Settings.MaxDistance then
				update(script.ClosestModel.Value)
				Chasing = true
				Humanoid.WalkSpeed = Settings.Speed
			else
				Humanoid.WalkSpeed = 0
				Chasing = false
			end
		end]]
		
		if Mag < Settings.AttackDistance and CanSeeTarget(ClosestModel) then
			if not AttackFinished then
				return
			end
			local Hit = ClosestModel.HumanoidRootPart --HumanoidRootPart
			local Horse = Hit.Parent:FindFirstChildOfClass('Humanoid')
			if Horse ~= nil and Hit.Parent ~= Killer and not Hit.Parent:FindFirstChild('Killer') and not Hit.Parent:FindFirstChild('YemPathing') then
				AttackFinished = false
				Horse:TakeDamage(Settings.AttackDamage)
				if Settings.IgnoreForcefield == true then
					Horse.Health -= Settings.AttackDamage
				else
					Horse:TakeDamage(Settings.AttackDamage)
				end

				if Killer:FindFirstChild('Swing') then
					Humanoid.Animator:LoadAnimation(Killer.Swing):Play() 
				end
				--
				local PickedSound = math.random(1, 3)
				if PickedSound == 1 then
					local NewSound = Instance.new('Sound')
					NewSound.Parent = Killer.HumanoidRootPart
					NewSound.Volume = Settings.AttackVolume
					NewSound.SoundId = AssetId .. Settings.Attack1
					NewSound:Play()
					Debris:AddItem(NewSound, 5)
				end
				if PickedSound == 2 then
					local NewSound = Instance.new('Sound')
					NewSound.Parent = Killer.HumanoidRootPart
					NewSound.Volume = Settings.AttackVolume
					NewSound.SoundId = AssetId .. Settings.Attack2
					NewSound:Play()
					Debris:AddItem(NewSound, 5)
				end
				if PickedSound == 3 then
					local NewSound = Instance.new('Sound')
					NewSound.Parent = Killer.HumanoidRootPart
					NewSound.Volume = Settings.AttackVolume
					NewSound.SoundId = AssetId .. Settings.Attack3
					NewSound:Play()
					Debris:AddItem(NewSound, 5)
				end
				task.wait(Settings.AttackDelay)
				AttackFinished = true
			end
		end
	else
		Chasing = false
	end
end)

This is the AI with the NoPathMoveTo

And this is the AI without the NoPathMoveTo