Coroutine not waiting for yield WaitForSeconds [JS]

I have been working on a Sanity level of a player and I’ve been using Coroutines to drain 5 sanity every second and that Coroutine I am using is just ignoring WaitForSeconds. I need help.

function SanityDraining(){
  
 if(lanternLight.enabled == false){
  
  StartCoroutine(sanityDrain(1.0));

 }else{
 
   StopCoroutine(sanityDrain(1.0));

  }
 
}

function sanityDrain(waitTime : float){
   playerSanity = playerSanity - sanityLoss;
   yield WaitForSeconds(waitTime);
}

I have called the Sanity Draining function in Update function and its working, everything is working but its just ignoring yield. I have a script that I’ve done with players light, also using coroutines and it works perfectly. I dont know what seems to be the problem here.

Are you expecting the Coroutine to continue draining the sanity over time? That’s what would happen if you’re calling that in update. But as a Coroutine it’s only going to subtract sanityLoss one time, wait for 1 second, then quit. If you want it to continue draining over time then the Coroutine needs to be a loop…

function sanityDrain(waitTime : float)
{
  while (playerSanity > 0)
  {
    playerSanity -= sanityLoss;
    yield WaitForSeconds(waitTime);
  }
}

This will loop while sanity is greater than 0, waiting for waitTime seconds for each iteration.