at a glance your while loop is set to go on forever
granted i dont know much but you might want to do while ($nick(%chan, %i) <= $nick(%chan, 0)
then it runs through the routine until it has looked at everyone in the channel