Agent stops following when player touches area which isn't on navmesh

Hi guys, I’ve been stuck on this for quite awhile. Whenever the player touches the area with a gap in the navmesh - such as around a table - The enemy agent stops moving toward the player (they are still in the follow state and know the coodinates of the player). I’ve tried to use samplePosition and this still doesn’t seem to work. Any ideas?

using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(EnemyController))]
public class EnemyMobile : MonoBehaviour
{
    public enum AIState
    {
        Patrol,
        Follow,
        Attack,
    }

    public Animator animator;
    [Tooltip("Fraction of the enemy's attack range at which it will stop moving towards target while attacking")]
    [Range(0f, 1f)]
    public float attackStopDistanceRatio = 0.2f;
    public ParticleSystem[] randomHitSparks;
    public ParticleSystem[] onDetectVFX;
    public AudioClip onDetectSFX;

    [Header("Sound")]
    public AudioClip MovementSound;
    public MinMaxFloat PitchDistortionMovementSpeed;

    public AIState aiState { get; private set; }
    EnemyController m_EnemyController;
    AudioSource m_AudioSource;

    const string k_AnimMoveSpeedParameter = "MoveSpeed";
    const string k_AnimAttackParameter = "Attack";
    const string k_AnimAlertedParameter = "Alerted";
    //const string k_AnimOnDamagedParameter = "OnDamaged";

    void Start()
    {
        m_EnemyController = GetComponent<EnemyController>();
        DebugUtility.HandleErrorIfNullGetComponent<EnemyController, EnemyMobile>(m_EnemyController, this, gameObject);

        m_EnemyController.onAttack += OnAttack;
        m_EnemyController.onDetectedTarget += OnDetectedTarget;
        m_EnemyController.onLostTarget += OnLostTarget;
        //m_EnemyController.SetPathDestinationToClosestNode();
        m_EnemyController.onDamaged += OnDamaged;

        aiState = AIState.Patrol;

        m_AudioSource = GetComponent<AudioSource>();
        DebugUtility.HandleErrorIfNullGetComponent<AudioSource, EnemyMobile>(m_AudioSource, this, gameObject);
        m_AudioSource.clip = MovementSound;
        m_AudioSource.Play();
    }

    void Update()
    {
        UpdateAIStateTransitions();
        UpdateCurrentAIState();

        Debug.Log($"NavMesh Path Status: {m_EnemyController.m_NavMeshAgent.pathStatus}");

        float moveSpeed = m_EnemyController.m_NavMeshAgent.velocity.magnitude;

        animator.SetFloat(k_AnimMoveSpeedParameter, moveSpeed);

        m_AudioSource.pitch = Mathf.Lerp(PitchDistortionMovementSpeed.min, PitchDistortionMovementSpeed.max, moveSpeed / m_EnemyController.m_NavMeshAgent.speed);

        if (m_EnemyController.knownDetectedTarget != null)
        {
            Debug.Log("Known Detected Target Position: " + m_EnemyController.knownDetectedTarget.transform.position);
            Debug.DrawLine(transform.position, m_EnemyController.knownDetectedTarget.transform.position, Color.red, 1f);
        }

    }


    void UpdateAIStateTransitions()
    {
        switch (aiState)
        {
            case AIState.Follow:
                if (m_EnemyController.isSeeingTarget && m_EnemyController.isTargetInAttackRange)
                {
                    aiState = AIState.Attack;
                    m_EnemyController.SetNavDestination(transform.position);
                }
                break;
            case AIState.Attack:
                if (!m_EnemyController.isTargetInAttackRange)
                {
                    aiState = AIState.Follow;
                }
                break;
        }
    }

    void UpdateCurrentAIState()
    {

        Vector3 targetPosition;
        UnityEngine.AI.NavMeshHit hit;
        switch (aiState)
        {
            /*case AIState.Patrol:
                m_EnemyController.UpdatePathDestination();
                m_EnemyController.SetNavDestination(m_EnemyController.GetDestinationOnPath());
                break;
            */
            case AIState.Follow:
                // Find the closest point on the NavMesh to the player's position
                targetPosition = m_EnemyController.knownDetectedTarget.transform.position;
                if (UnityEngine.AI.NavMesh.SamplePosition(targetPosition, out hit, 1000f, UnityEngine.AI.NavMesh.AllAreas))
                {
                    // Logging the hit position
                    Debug.Log("NavMesh Hit Position: " + hit.position);
                    // If a point on the NavMesh was found, update the destination
                    m_EnemyController.SetNavDestination(hit.position);
                }
                else
                {
                    // Optionally, handle the case where no point on the NavMesh was found
                    Debug.Log("No point on NavMesh found near player");
                }
                m_EnemyController.OrientTowards(targetPosition);
                break;

            case AIState.Attack:
                targetPosition = m_EnemyController.knownDetectedTarget.transform.position;
                if (UnityEngine.AI.NavMesh.SamplePosition(targetPosition, out hit, 1000f, UnityEngine.AI.NavMesh.AllAreas))
                {
                    if (Vector3.Distance(hit.position, m_EnemyController.m_DetectionModule.detectionSourcePoint.position)
                        >= (attackStopDistanceRatio * m_EnemyController.m_DetectionModule.attackRange))
                    {
                        m_EnemyController.SetNavDestination(hit.position);
                    }
                    else
                    {
                        m_EnemyController.SetNavDestination(transform.position);
                        m_EnemyController.MeleeAttack();
                    }
                }
                m_EnemyController.OrientTowards(targetPosition);
                break;
        }
    }

    void OnAttack()
    {
        animator.SetTrigger(k_AnimAttackParameter);
    }

    void OnDetectedTarget()
    {
        if (aiState == AIState.Patrol)
        {
            aiState = AIState.Follow;
        }

        for (int i = 0; i < onDetectVFX.Length; i++)
        {
            onDetectVFX[i].Play();
        }

        if (onDetectSFX)
        {
            AudioUtility.CreateSFX(onDetectSFX, transform.position, AudioUtility.AudioGroups.EnemyDetection, 1f);
        }

        //animator.SetBool(k_AnimAlertedParameter, true);
    }

    void OnLostTarget()
    {
        if (aiState == AIState.Follow || aiState == AIState.Attack)
        {
            aiState = AIState.Patrol;
        }

        for (int i = 0; i < onDetectVFX.Length; i++)
        {
            onDetectVFX[i].Stop();
        }

        //animator.SetBool(k_AnimAlertedParameter, false);
    }

    void OnDamaged()
    {
        /*if (randomHitSparks.Length > 0)
        {
            int n = Random.Range(0, randomHitSparks.Length - 1);
            randomHitSparks[n].Play();
        }

        animator.SetTrigger(k_AnimOnDamagedParameter);*/
    }
    
}