How does NAN happen?

I know that NAN stands for “Not a Number,” but I’ve been having issues with data for a large game where cash mysteriously gets decimal points and sometimes even NANs when no impossible math is being done.

I’m curious to get insight as to how it can happen other than impossible math operations, because its causing a lot of problems with my data storage.

When you have a math operation that results in undefined, like 0/0 or math.acos(2).

You should always make checks before storing.

If you’re doing math that involves float values (0.5, 1.1, etc.) it will always lack precision up to a certain point. 0.3 will be stored is 0.3000039819 for example. Consider the following script:

local a = 0.1 + 0.2
if (a == 0.3) then
  print'yes'
else
  print'no' -- Surprise, this gets printed
end

So it’s possible that a function you used on the number is making Lua treat it as a float value and is storing it as 1.00001 rather than just 1

There’s no operations that will result in an undefined in the game, I verified that. My ask was for reasons other than undefined math operation

Are you sure? A client can pass NaN or an operation that can result in NaN to the server.

The client has no control of anything related to data. I’m positive NAN can’t happen via client or server due to undefined, at least for the data

1 Like

Are you suggesting that this NAN could be caused by a floating point error?

No, I wouldn’t think that would be able to happen.

Not sure then… floating point operations aren’t my strong suit.

Have you done any type conversion perhaps? cough cough tostring(), tonumber() cough cough

Could this work if its somehow undefined? I don’t think tonumber can convert an undefined integer

Quick devforum search shows that when checking tonumber() for NaN it doesn’t work, but I will test it in a script and see if this was just a fluke of bad programming. I would assume tonumber() would either throw an error or produce NaN.

Edit: So far tonumber() returns nil if it is a bad operation.

Edit 2: I got a nan when doing 0/0 and a inf when doing 2/0, so it could be tonumber() from a bad string that is doing it. Not sure your set up though.

I have had problems with NAN popping up in my game. It seems to happen with mobile players for me, I use a swimming and flying code that is taken from a Roblox gear.

PC users have no issues, but mobile users get NAN on occasion, even when using the original Roblox gear.

What I try to do is to check for it, or check for a fail in a pcall using the number in a calculation, and then try again with slight adjustments

What I have been told and what I use to check in my game is you do …
if value ~= value then print(“NAN”) end

4 Likes

This sorta lines up to be honest. For the game, our sniper class had an issue where the gun just disappeared for mobile players. We fixed it but it just shows roblox has bad systems for mobile that can cause a lot of issues.

2 Likes

I had such issues with it and trying to find a way to check for nan and fix it, I almost got to the point where I was going to make my game closed to mobile. If it wasnt for a few dedicated mobile players to bug test with me over the course of a few days, I would have removed mobile support.

So it turns out that this did not fix the issue. We installed a quickfix to stop people from losing all data but they still lose a considerable amount of cash.

So, seeing as I have resolved the issue with the help of my community, I will tell why this happened and how to avoid it

basically, players were leaving before the data could properly set or load. The fix I put in place stops data from loading or saving until the game is fully loaded, and also adds rewards before they have the chance to leave the game. Be careful with data guys, it can really screw stuff up.

1 Like

I’m not sure if this is commonly used but if I have a script that involves me needing to do something similar to this I use if (a =< 0.3) then instead of if (a == 0.3) then