So, I want to make a live event that plays every 1 hour. It means that when the real life time reached xx:00, the event will play.
Is there any topic that this might solve the problem or just anyone know how to do this? Thank you!
So, I want to make a live event that plays every 1 hour. It means that when the real life time reached xx:00, the event will play.
Is there any topic that this might solve the problem or just anyone know how to do this? Thank you!
Try this:
while true do
local t = os.date("!*t")["min"] -- this is the current minute, e.g. xx:50 --> 50
if t == 0 or t == 00 then -- used 0 and 00 because I'm not sure whether it'll return 0 or 00.
print("time is xx:00")
end
wait(60) -- run above code every 60 sec (1 min)
end
You can do something like this:
local db = false
game:GetService("RunService").Heartbeat:Connect(function()
if (os.date("!*t").min == 0 and os.date("!*t").sec == 0) and not db then
db = true
-- DO THE EVENT HERE
task.wait(1)
db = false
end
end)
This may not work properly as it fires every minute after the Server has initialized, so if it has initialized on something like xx:xx:35 then the event will fire once it’s xx:00:35 and that’s not what the OP asked for, he asked for it to be always xx:00:00
He asked for xx:00, which is the 0th minute. I agree mine will not run at exactly xx:00:00, however he did say xx:00 and not xx:00:00. My solution requires less frequent looping and whatnot.
You are right, tho I think that it would make more sense if it was xx:00:00.
Hi, so actually I apologize for not including this on the post I made, but how can I make a countdown with the sample codes above?
I agree, lets say both are solutions are equally amazing and he can choose which he wants?
Hi, yes, both of them are actually nice and it helps me a lot!
That’s surprisingly easy to do, all you need is to do something like this
local db = false
local countdownString
game:GetService("RunService").Heartbeat:Connect(function()
local countdownString = ("%s:%02i"):format(60 - os.date("!*t").min - 1, 60 - os.date("!*t").sec - 1)
print(countdownString)
if (os.date("!*t").min == 0 and os.date("!*t").sec == 0) and not db then
db = true
-- DO THE EVENT HERE
task.wait(1)
db = false
end
end)
@wGummi yes
Disconnect the event instead of applying a debounce once it the countdown strike 0.
It will not work -it will work for the first time only, for example it will work when it gets to 17:00:00, but when it gets to 18:00:00 it will not work anymore because I disconnected the event.
Assuming you want it to fire on minute and second zero, this is a solution that doesn’t require checking the time using a loop(it calculates the time until the hour change on the first iteration, and uses a thread for the loop code so it doesn’t get delayed):
--Calculates the time until the hour changes
function TimeUntilHourChange(f: string): number
local d = os.date(f)
return 3600-d.minute*60-d.sec
end
--code that runs per hour
function PerHourCode()
print("Hi, the hour just changed :)")
end
task.wait(TimeUntilHourChange("!*t"))
--why true instead of task.wait() on top of the loop?
--because the first iteration doesn't wait for 3600 seconds!
while true do
--to fix the potential error created by the time task.spawn takes to run
local t = os.clock()
--why task.spawn?
--because the code should not delay the timer!
task.spawn(PerHourCode)
task.wait(3600+t-os.clock())
end
In fairness it’s done on a loop, but at least I gave some context when you want it to happen once.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.