Actually, everything runs correctly... You're just not observing that the following lines of code are actually executing prior to your even starting the "QLeader" command itself. Just follow the command line:
Code:
(defun c:gt (/ lastEnt ss)
(setvar "qaflags" 1)
(setq lastEnt (entlast)
ss (ssadd)
)
(command "Qleader")
(prompt "\n** This line runs before your \"qleader\" command ends ** ")
(while (setq lastEnt (entnext lastEnt))
(ssadd lastEnt ss)
)
(sssetfirst nil ss)
(COMMAND "EXPLODE" ss)
(setvar "qaflags" 0)
(setq ss nil)
(setq lastEnt nil)
(princ)
)