Hi, so I’m going to be making a system where it determines if you’ve played the game for over a month, then you get a reward.
I figured the easiest way would be to do something like…
local last_time_visited = player_time --//Being a saved os.time() value
if (os.time() - last_time_visited) >= (60 * 60 * 24 * 31) then print'yep' end --Should be a month worth of seconds if I wrote that correctly
I’ve never tried this. With numbers so large, it automatically converts to exponential notation. Would an arithmetic operation like this work incorrectly when comparing the previous value to such a large number? Thanks.
I mean if you are really concerned about that, then you can just divide os.time() and last_time_visited.
local last_time_visited = player_time --//Being a saved os.time() value
if (os.time()/60 - last_time_visited/60) >= (60 * 24 * 31) then print'yep' end -- Should be a month worth of minutes
I also just tried the code myself (the one you posted) and it worked perfectly fine, so there should be no problems. I also tried it with 60 * 60 * 24 * 31 * 12 which is a year worth of seconds and it worked perfectly fine.
The exponential notation is just a way of formatting the numbers to be more clearly visible in things like print messages. The internal representation of the numbers is still the same so unless you’re getting close to the floating point limits you shouldn’t face any problems with arithmetic. This Wiki page details more information in the FP type Lua uses.
Some experimenting in studio showed me that numbers don’t start getting formatted into scientific notation until you reach about 10^14, which is more than 60000 times larger than the numbers you would expect to be returned by functions like os.time(), so I wouldn’t be worried.
print(10000000000000) -- 10000000000000
-- 10^14 is the cutoff apparently
print(100000000000000-1) -- 99999999999999
print(100000000000000) -- 1e14
print(100000000000001) -- 1e14
print(1e14 == 1e14 + 1) -- false
print(1e14 / os.time()) -- 63187.07....
-- A timestamp of 10^14 won't happen until the year AD 3 million
local year = 365 * 24 * 60 * 60
local sec = 1e14
print(math.floor(sec / year) + 1970)-- 3172949