I unfortunately don't have that much time to go through your entire code but what actually like immediately caught my eyes is your use of global variables within function loops.
That's a big nono. Always try and make as many variables as possible local!
Im not saying that this fixes your error, as I mentioned above, but I'm saying that it's a good idea to make your gi_playernum and gi_playercnt local.