Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I detect if a number is nil?

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

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.

2 comments

0

Thank you so much! This helped a lot!

0

Remember you can mark answers as accepted so that other people finding your problem more immediately find the answer...

This discussion is preserved as it was. New answers, comments, and votes are closed.