How would I script a countdown timer with hours, minutes, and seconds with changeable values?

  1. What do you want to achieve?
    I want to make
    a Timer that automatically counts down from hours, minutes, and seconds,
    (00:00:00) with changeable values for them in the script.
  2. What is the issue?
    cannot find anything related to setting a timer like said and having it count down.
  3. What solutions have you tried so far?
    the solutions either don’t work, have non changeable values, and/or are related to world time / Unix time, if that makes sense, which is NOT what I need.

I’ve been working on a horror game, and in the game, it has one character that starts a timer before disappearing once it ends, but I’ve not been able to figure out a way to make it spawn in with a random time value, and then countdown from it. other than that, everything else about the character works as intended.

I appreciate any attempts at helping!

local hours = 1
local minutes = 30
local seconds = 0

while true do
	if hours == 0 and minutes == 0 and seconds == 0 then
		break
--	else
--		print(string.format("%02d:%02d:%02d", hours, minutes, seconds))
	end

	seconds = seconds - 1
	if seconds < 0 then
		seconds = 59
		minutes = minutes - 1
	end

	if minutes < 0 then
		minutes = 59
		hours = hours - 1
	end

	wait(1)
end

print("Countdown complete!")

a very basic countdown.

There’s a plethora of ways to do this, what I would do to see the time that has elapsed is using tick() and capturing that at the beginning of the timer (when it starts), then just use a while loop.

Ticks give the amount of seconds since the epoch.
Example:

local startTick = tick()
local TimerTotal = 300 -- Put this in seconds

while (tick() - startTick) < TimerTotal do
--- Timer code here.

task.wait(1) -- Wait one second
end

Account for a one second difference at the end, so if there is 300 seconds you’d start the timer at 301

So a full blown working example of what you’re asking for is here:

local startTick = tick()
local TimerTotal = 120 -- Total time in seconds

function formatString(Int)
	return string.format("%02i", Int)
end

function convertToHMS(Seconds)
	local Minutes = (Seconds - Seconds%60)/60
	Seconds = Seconds - Minutes*60
	local Hours = (Minutes - Minutes%60)/60
	Minutes = Minutes - Hours*60
	return formatString(Hours)..":"..formatString(Minutes)..":"..formatString(Seconds)
end

while (startTick - tick()) < TimerTotal do
	local TimeDifference = math.round(startTick - tick())
	local TimeLeft = (TimerTotal + 1) - TimeDifference
	local Timer : TextLabel =  nil -- path to your TextLabel/Output

	Timer.Text = convertToHMS(TimeLeft)

	--- Gives you 00:02:00, if you plug in 120 seconds
	task.wait(1) -- Wait one second
end

Just edit the TimerTotal

You also mentioned making the value random, in this case you’d just do:

local TimerTotal = 0
TimerTotal = math.random(minSeconds, maxSeconds)
2 Likes

it works, although it just hangs at 00:00:01 thanks alot!

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