Then you find out why, in a systematic debugging process.

Programmers do that all the time. Beginners are not used to it, so it's difficult to them. That's why I suggested that you post your problem in a separate thread, and I'll help finding the reason, step by step. This way other users also can learn from it. You're not alone: anyone writing scripts will encounter such problems sooner or later.