Issue with a countdown timer

I have created the following script, which is supposed to work as a five-minute-countdown timer. However instead of doing that, it counts down five seconds. Additionally, the timer doesn’t work the way it’s meant to in terms of the format (Minutes:Seconds:Hundereds), but like Seconds:Sixties:00, which is an absolute mess. Any help would be appreciated.

local RunService = game:GetService("RunService")

local label = script.Parent
local function startTimer(timeLeft)
    repeat
        RunService.Heartbeat:Wait()
        timeLeft -= 1
        local minutes = math.floor(timeLeft / 60)
        local seconds = math.floor(timeLeft % 60)
        local hundereds = math.floor(timeLeft % 1 * 100)

        if timeLeft >= 0 then
            label.Text = string.format("%02i:%02i:%02i", minutes, seconds, hundereds)
        end
        if timeLeft <= 30 then
            script.Parent.Script2.Enabled = true -- Misc script for something else
        end
    until timeLeft == 0
end

startTimer(5*60) -- Begins counting down five minutes

@TheHazardCz


First thing you need to copy this script:

local countdownGui = script.Parent
local countdownText = countdownGui:WaitForChild("CountdownText")
 
 
local day = os.time({year = 2020, month = 8, day = 19, hour = 11, min = 16, sec = 0})
 
 
while wait() do
    
    local secondsBetween = os.difftime(day, os.time())
 
    local seconds = secondsBetween % 60
    local minutes = math.floor(secondsBetween % (60*60) / 60)
    local hours = math.floor(secondsBetween % (60*60*24) / (60*60))
    local days = math.floor(secondsBetween % (60*60*24*30) / (60*60*24))
    
    local textString = days .. "d:" .. hours .. "h:" .. minutes .. "m:" .. seconds .. "s"
    countdownText.Text = textString
    
    if secondsBetween <= 0 then break end
end

And add these things:


and bye

I don’t think you understood my issue and what I am looking for at all.
First of, I do not see how current date is relevant - I am not trying to count down to specific date, but generally count down 5 minutes, secondly, I am not looking for having days in the countdown and finally, I am looking for a way to patch the current script, not have completely different one, should it be feasible.

heartbeat takes on average 1/60 seconds (it fires every frame)

so every 1/60 seconds you are subtracting 1 second from the timer. that works out to taking 1 minute off the timer every 1 second. funny coincidence lol

instead of taking off 1 second per heartbeat, take off the delta time (time waited by the heartbeat:Wait() ):

local dt = RunService.Heartbeat:Wait()
timeLeft -= dt

also since the count down can overshoot 0 and give a negative value for timeLeft, wait until timeLeft is less than or equal to 0

until timeLeft <= 0

1 Like
local countdownGui = script.Parent
local countdownText = countdownGui:WaitForChild("CountdownText")
 
while wait() do
    
    local secondsBetween = os.difftime(minutes, os.time())
 
    local seconds = secondsBetween % 60
    local minutes = math.floor(secondsBetween % (60*60) / 60)
    
    local textString = minutes .. "m:" .. seconds .. "s"
    countdownText.Text = textString
    
    if secondsBetween <= 0 then break end
end

I’m not sure…

why instead of using runservice:Wait() instead use task.wait()

That did it - cheers, thanks man

1 Like

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