i have a script that gives player a perk for 24hours. sometimes it doesn't work because the request is taking too long and returning nil i think. i made some prints at key points throughout my script and sometimes it returns to me the print for "timed out" which means it's not getting the time. i have tested this and it works if it returns the time, but it's not always returning the time. i'm using
game:GetService("HttpService"):GetAsync("http://www.timeapi.org/utc/now?%25Q", true)
to record the time and then using it again later to check the time. i'm not sending a bunch of requests. it only makes the request once during purchase, and once when player either enters the game or resets their character. like i said it all works fine except i think like today there was alot of traffic on the internet and it was just not receiving the request.
so, is there a way for my to get the time on/from the game server?
The problem is that the site is too slow. You could check if downloading the time worked and repeat until it worked correctly, but it would be a lot more efficient to use os.time() like M39a9am3R mentioned, which does the same. On the wiki it also mentions the option to call it with a table, but in this case you can simply do this:
local expirationTime = os.time() + 24 * 3600
Then you can store this somewhere if you want. Since expirationTime
is a number instead of a string like you would've gotten if you used HttpService, to check if the perk has expired, use this:
if os.time() >= expirationTime then -- The item has expired. end
Because os.time() always returns the time in UTC, it will be independent of the server you use it on and it will give you the same result as you would've gotten if you had used timeapi.org instead.