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
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.
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
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.
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