Code:
on *:text:!whois:#:{
  var %a = 1
  while %a <= $nick(#,0) {
    .enable #scan
    whois $nick(#,%a)
.timer 1 2    inc %a
  }
  set %uptime $sort(%uptime,32,n)
  var %a = 1
  while %a <= $numtok(%uptime) {
    set %nick $addtok(%nick,$hfind(uptime,$gettok(%uptime,%a,32),1),32)
    if %a < 5 { .write -s $+ $nick uptime.txt $nick $gettok(%uptime,%a,32) }
    inc %a
  }
}

#scan off
raw 317:*:{
  hadd -m uptime $nick(#,%a) $calc($ctime - $4)
  set %uptime = $addtok(%uptime,$calc($ctime - $4),32)
}
#scan end
 


oops...forgot about the timer..one is now included, and also noticed that I missed some brackets and the end command for the scan. I hope that'll work for you now