From what you've shown, it looks to me that the problem is in your greetuser alias. Post that and we might be able to figure out why it sometimes doesn't work.
As a suggestion, you can reduce your script to
on !*:join:#:{ greetuser # $nick }
The ! in the prefix section of the ON JOIN event means "anyone but me"