Here's a super simple solution.
Seeing as you're using proximity detectors and looping script commands, Just make the first section of the script check if rel:0,0,0 is emitting power. I am fairly sure that's a conditional. If it isn't, Just check if it's powered.
This way you aren't canceling any scripts, So it should work just fine.
When there are no players nearby, It will just stop looping the script.