IMHO code looks fine. The only thing I could imagine is that "player" becomes invalid at a certain time.
So a "if (!player) break;" after each "wait(1)" might help.

Beside that I would recommend to have only one while loop and track a state instead of having cascading whiles. But thats more a design philosophy and should not be the cause of the error.