If your loop crashes the game, the most likely cause it that it doesn't end. Double-check the condition to end the loop. (Try adding a show_debug_message() inside the loop to see if it actually does anything - if you get a million printouts, you've confirmed it!)