Trying to make Ai escape script

Hi, I was trying to make a simple ai escape script what I was trying to make is an enemy that goes to some patrol points and when it gets close to the player it stops for a few seconds and it flees I have code but it doesn’t work properly there were some few bugs like it doesn’t flee properly its kinda laggy and the speed shifts

public float speed;
    public Transform target;
    public float minimumDistance;

    public Transform[] patrolPoints;
    public float waitTime;
    public int currentPointIndex;

    public bool once;

    public bool GoToPatrolPoint;

    public bool run;

    public Animator anim;
void Update()
    {
 if (run)
            {
                GoToPatrolPoint = false;
            }
            StartCoroutine(GoandStop());
            if (transform.position != patrolPoints[currentPointIndex].position && GoToPatrolPoint)
            {
                transform.position = Vector2.MoveTowards(transform.position, patrolPoints[currentPointIndex].position, speed * Time.deltaTime);
                anim.SetBool("isMoving", true);
            }
            else
            {
                anim.SetBool("isMoving", false);
                if (once == false)
                {
                    StartCoroutine(Wait());
                    once = true;
                }
            }
}
IEnumerator GoandStop()
    {
        if (run)
        {
            speed = 0;
            anim.SetBool("SawPlayer", true);
            yield return new WaitForSeconds(.35f);
            anim.SetBool("SawPlayer", false);
            speed = .8f;
            Vector3 dir = transform.position - target.position;
            transform.Translate(dir * speed * Time.deltaTime);
            yield return new WaitForSeconds(1f);
            GoToPatrolPoint = true;
            run = false;
        }
        else
        {
            speed = 3f;
        }
        

    }
    IEnumerator Wait()
    {
        yield return new WaitForSeconds(waitTime);
        if(currentPointIndex + 1 < patrolPoints.Length)
        {
            currentPointIndex++;
        }
        else
        {
            currentPointIndex = 0;
        }
        once = false;
    }

You should go 3 Steps back and rethink your approach to this. Your current code seems extreme complex for the task that you are trying to achive…

To start with try to add a proper state to your Script:

  public enum Ai_State { Idle, Patroling, Fleeing }
  public Ai_State currentState;
  public bool stateChangedThisFrame = false;

Then try to not start Coroutines each Frame. Only do that when it is actually needed.
Then you can have a switchcase for the current state in your Update:

    switch(currentState) {
            case Ai_State.Idle:
                   doIdleBehaviour();
            case Ai_State.Patroling:
                   doPatrolingBehaviour();
             //.... And so on....

And add a function:

   ///use this to change the state of your Ai so that you always have a clean change where you also set the stateChanged flag.
   void changeState(Ai_State newState){
          if(newState == currentState)
                 return;
          currentState = newState;
          stateChangedThisFrame = true;
   }

Then you can have functions for each of these behaviours.
To enable single calls of behaviours use the stateChangedThisFrame flag.
In your doXBehaviour functions check for the stateChanged flag, here you can then for example start any timed behaviour in a coroutine. set the stateChangedFlag to false here.

Only thing that should be missing then is that you have to stop Coroutines that are already running. For example if you stop the Fleeing early and want to go back to patroling you have to stop the fleeing coroutine.

hope this helps, let me know if something is not clear. In any case try to add more details next time please.