while (myPlayer().isAnimating()) {
Don't use while loops in your scripts. The only while loop you should be using is the onLoop method that you're forced to override after extending script. Since your script is already looping, you could easily use an if statement to check if you're animating.