• You've discovered RedGuides, an EverQuest multi-boxing and scripting community 🧙‍♀️⚙️. We want you to play several EQ characters at once, come join us and say hello! 👋

  • A TLP without truebox has thawed (Very Vanilla ready)
    Frostreaver

Question - How to avoid comparing nil with a number crashes

Soandso2

Well-known member
Joined
Mar 13, 2023
RedCents
937¢
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.

[CODE lang="Lua" title="nil function"]
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[/CODE]
 
Question - How to avoid comparing nil with a number crashes

Users who are viewing this thread

Back
Top
Cart