Your wait() is within the if-branch.
So there is an endless loop if the condition (character_pan.pos_x > 800) is not met.