This line was also incorrect:

while ( %nicks <= $nick(%chans,0) ) {

It should be this:

while ( %nicks <= $nick($chan(%chans),0) ) {


-genius_at_work