How can I repeat a function every one hour?

Hello fellow devs!

I am planning to start a 3 minutes firework event every o’clock on coming 2 days.

The firework show is enclosed in a function, so I want to start it every one hour.
(like 7:00 am, 8:00 am, …)

I know while loops and task.wait() but it seems to be too long to wait an hour.
Also I know Datetime:Now() to get current os time, but I dont know how to use this to schedule the event occur every one hour.

I appreciate if someone help me.

2 Likes

You could do something like this:

local function startFireworkShow()
    -- Your code
    print("Test");
end

while true do
    local currentTime = DateTime.now():ToUniversalTime();

    local minutes = currentTime.Minute;
    local seconds = currentTime.Second;
    
    -- Calculates the time remaining until the next hour
    local waitTime = (60 - minutes) * 60 - seconds;

    task.wait(waitTime);

    startFireworkShow();
end
3 Likes

I confirmed it works properly.
Thank you so much!

1 Like

Thank you for your reply.
Your code seems to be good too!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.