How to Save numbers over 9.223 Quintillion to OrderedDataStore?

What I did was use a bit of math to get around it.

local value = 9.99e50
local storedValue = value ~= 0 and math.floor(math.log(value) / math.log(1.0000001)) or 0

print(storedValue) -> -- 1174308450
local value = ...
local retrievedValue = value ~= 0 and (1.0000001^value) or 0

print(retrievedValue) -- 9.9899995470475e+50

The idea is that you shrink the number and stretch it again afterwards, this WILL cause a loss in precision. But with huge numbers that are most likely rounded for leaderboards anyways that’s not an issue. The larger the number you’re trying to store the less precise it’ll be. So for smaller numbers it’ll be accurate.

You perform the first math operation and save the value to the DataStore, then when you load the value you perform the second math operation. Sorting will still work for this because the numbers still represent the original value correctly.

Also make sure not to pass 0 into the equation, or you’ll get a “division by 0” error. Hence why I used a ternary operator to make sure it’s not 0. Since 0 can just be stored and loaded as 0.

57 Likes