Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I detect if a number is nil?

Asked by 10 years ago

Ok so I have a script that returns the a UnitRay of the Mouse, and when you press the the "/" key to chat, the UnitRay becomes NAN,NAN,NAN. I've tried many ways to tell if the UnitRay is nil, but none of them work and my script ends up erroring. Is there any way to tell if the number is nil?

Note: I've tried tonumber(UnitRay), pcall(function() local Test = 2 * UnitRay end), and other methods

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

A number cannot be nil - it's a number.

NaN's name is "not a number" . It is a number value nonetheless.

(NaN is C's name for it; Lua calls it -1.#IND)

NaN is a number; NaN is not nil.


An easy way to check if a value x is NaN is to compare it to itself; this is because NaN ~= NaN.

function isNaN(x)
    return x ~= x;
end

Note that this extends to Vector3 values; if any component is NaN then this function will also return true, but otherwise false.

0
Thank you so much! This helped a lot! TurboFusion 1821 — 10y
0
Remember you can mark answers as accepted so that other people finding your problem more immediately find the answer... BlueTaslem 18071 — 10y
Ad

Answer this question