Theres an event for disconnection , I'm not shure it was event_disconnect exactly

look the manual. With the event , you dont need to know wich client disconnected.
About the other problems , I'd suggest you start again. I've written my MMO scripts over 15 times

and the last one looks clean and works good and I know how to make it again in less than a day...