Need support on my basic navmesh ai script to follow the player at a certain distance

I know this is kind of a noob question but I really need some quick help, I have this simple AI script that allows an object to follow the player on a navmesh, but for the life of me I cant figure out how to get the object to only follow me when I am in a certain distance of it, and when I am out of reach, the object will stop following the player. The javascript i have is below.

var target : Transform;
var navComponent : NavMeshAgent;
var anim : Animator;
 
function Start () {
target = GameObject.FindWithTag("Player").transform;
        navComponent = this.transform.GetComponent(NavMeshAgent);
        anim = GetComponent("Animator");
}
 
function Update () {
 
        if(target) {
       
                navComponent.SetDestination(target.position);
                anim.SetFloat("speed",1);
        }
}

Here’s some spoon feeding for you:

 var target: Transform;
 var navComponent: NavMeshAgent;
 var anim: Animator;
 var followDistance: float; // the distance in which to follow the player
 
 function Start() {
     target = GameObject.FindWithTag("Player").transform;
     navComponent = this.transform.GetComponent(NavMeshAgent);
     anim = GetComponent("Animator");
 }
 
 function Update() {
     if (target) {
         var distanceToTarget = Vector3.Distance(transform.position, target.transform.position);
         if (distanceToTarget <= followDistance) {
             navComponent.Resume();
             navComponent.SetDestination(target.position);
             anim.SetFloat("speed", 1);
         } else {
             navComponent.Stop();
             //anim.SetFloat("speed", 0);
         }
     }
 }