• Hail, we're RedGuides. We want you to play six (yes, 6!) EverQuest characters at once. We have scripts and a friendly community to help, please join our playstyle and say hello! 👋
  • We've added LLM sections to our Terms & Rules and Resource Rules, for everyone's sanity & in-game safety.
    Read more
    a. Do not replace your voice with an LLM. This goes for your posts and your scripts. Drafting and editing with a tool is fine, but don't take you out of what you share.
    b. Do not replace your EQ character's voice with an LLM. We don't want scripts that speak over /ooc, /shout or other public channels. This has been an unwritten rule that's kept casino scripts off the site for decades, but we're now compelled to make it clear: keep automated communication private.

    Why Does RG Review Scripts?
    Nearly every LLM-written Lua script we see is making your character do impossible things, and rarely in a way that's beneficial to you. We've seen heavy server hammering, /target over impossible distances when /nav spawn would passively accomplish the same thing, a spawn search that fires 20+ times per second, and other pointless actions that are likely annoying GMs.

    You are welcome to use LLMs as a tool to structure, draft, and edit your work, but please read critically before hitting /lua run

Question - How to avoid comparing nil with a number crashes

Soandso2

Well-known member
So, my main loop starts like this:

Lua:
while true do
    if me.Combat() and target() then
        if not stopBellow and target.PctHPs() < 90 and me.AltAbilityReady("Boastful Bellow")() and target.Buff("Boastful Bellow L").ID() == nil then

But it used to start like this:

Lua:
while true do
    if me.Combat() and target() and target.PctHPs() < 90 then
        if not stopBellow and me.AltAbilityReady("Boastful Bellow")() and target.Buff("Boastful Bellow L").ID() == nil then

The second version crashed multiple times due to "compare nil with a number", in the second if-statement. But after having changed it to the first version... no more crashes.
The "compare nil with a number" error occurs when the target has died and target.PctHPs() result in nil instead of it's current HP.

First of all, what is wrong with the second version of the code?

And is there a way to prevent "compare number with nil" if the target dies before the script actually tries to evaluate the condition?

EDIT: Forgot to mention that me and target in the code are shortened versions of mq.TLO.Me and mq.TLO.Target etc. The Lua WORKS, I am just showing the parts that sometimes crash.
 
always try to assign a default fallback incase of a nil value. you can get these especially when zoning
Certainly. I just did not know that you could do that. But when I see the solution above I understand how it works. I get nil values every now and then here and there, and will change all code accordingly to this solution. :)
 
You can also just make a universal nil function to catch anything. Just stick towards the top of the script.

nil function
Lua:
local function avoidNil(the_check)
  return the_check or 0
end

while true do
    if me.Combat() and target() and avoidNil(target.PctHPs()) < 90 then
        if not stopBellow and me.AltAbilityReady("Boastful Bellow")() and target.Buff("Boastful Bellow L").ID() == nil then
 
Question - How to avoid comparing nil with a number crashes

Users who are viewing this thread

Back
Top
Cart