How to check if 1 hour has passed

Hi, i’m not too sure how to check if 1 hour has passed, i have posted my code below, I heard that you’d have to create an offset which i did but im not too sure how to apply that offset, im trying to check if 1 hour has passed then it sets this variable to false, im creating a system where, when a webhook is POSTED, it’s on an HOUR cooldown to prevent new servers from posting the webhook again and to make it post just once. Here is my code:

local Offset = (60 * 60 * 1) --1 hour

while true do

	local BannerKey = "Banner"
	local WebhookPosted
	local TimeNow = os.time()
	local success, err = pcall(function() --Gets key and data, or set the data
		BannerStats = BannerDSS:GetAsync(BannerKey) or {BannerCD = false, Timer = 3600}
	end)

	if BannerStats then
		WebhookPosted = BannerStats.BannerCD --Getting CD		
		BannerTime = BannerStats.Timer
	else
		BannerDSS:SetAsync(BannerKey, {BannerCD = false, Timer = 3600})
	end

	
	if (os.time() - BannerTime) >= 3600 then --getting the time from the datastore to see if 1 hour has passed
		WebhookPosted = false
	end
	if not WebhookPosted then 
		local Data = {
			--webhook information--...
		}
		HTTPService:PostAsync(WebHookURL, HTTPService:JSONEncode(Data))
		local success, err = pcall(function()
			BannerDSS:SetAsync(BannerKey, {BannerCD = true, Timer = os.time()}) --Set CD to true and set a timer now (this timer is meant to be used to check the difference from when it was last posted)
		end)
	end
	wait(10)
end			

You could store the tick at the start of the server and then add the offset to that and wait for that number to be less than the current tick

I’d use tick() instead of os.time(). It returns a number which says how many seconds have passed since the unix time stamp (00:00:00 UTC 1st of January 1970).

When you need to check if an hour has passed you should then do tick() - storedtick and check if the returned value is higher than 3600 seconds.

local HourPassed = tick()+(60 * 60 * 1)

while true do
    if tick() > HourPassed  then
          print("1 Hour Passed")
    end
    --Other code
end

Thank you wait so, in my case would i have to make the HourPassed like this:

local HourPassed = BannerTimer+(60 * 60 * 1)  --Banner time from the datastore

or do i not need to save the timer in datastore anymore ? (I was saving it to keep track of when it was last posted) or will offset take care of this ?

so like:


local HourPassed = tick()+(60 * 60 * 1)

while true do

	local success, err = pcall(function() --Gets key and data, or set the data
		BannerStats = BannerDSS:GetAsync(BannerKey) or {BannerCD = false, Timer = 3600}
	end)

	if BannerStats then
		WebhookPosted = BannerStats.BannerCD --Getting CD		
		BannerTime = BannerStats.Timer
	else
		BannerDSS:SetAsync(BannerKey, {BannerCD = false, Timer = 3600})
	end

    if tick() > HourPassed  then
          print("1 Hour Passed")
         local success, err = pcall(function()
			BannerDSS:SetAsync(BannerKey, {BannerCD = true, Timer = os.time()}) -- saving the cd to data and time since it last posted (now) 
		end)
    end
    --Other code
end

I was told before that i shouldnt be using tick since its deprecated but idk what replacement i should use for this case

tick is not depreciated idk who told you that

You should be using os.time(), not tick(). The former gets the time since the unix epoch in UTC time, whereas tick() gets it relative to the machine it is called on. So this makes tick() unreliable since you could end up in a server in a later or earlier time zone. os.time() won’t have this issue at all.

1 Like

Tick is unreliable and deprecated. You should be using os.clock()

(highlighted for emphasis)

it will be deprecated eventually.

1 Like

os.clock is for benchmarking, not getting unix time. So this doesn’t work either.

local start = os.clock()

RunService.RenderStepped:connect(function()
if os.clock()-start > 3600 then
— an hour passed
end
end)

Oh I did not know that. Thanks for bringing it up

Wait so guys, should i completely scrap saving the time it last posted in the data store? and just set a timer at the top of the script, will the offset make it so different servers at the same time run in sync and keep track of the time at the same time?, I was storing in datastore to prevent different servers having their “own” time