24-hour quest countdown issue

So I’m currently creating a 24 daily quest countdown and I got it to work but the problem is that it’s counting up instead of down. Here’s my code:

local originalTime = os.time() + 86400

local function toHMS(s) --convert to hours, minutes, seconds and format to 0 left-padded integers separated by colons. 
	return ("%02i:%02i:%02i"):format(s/3600, s/60%60, s%60)
end

PlrDailyQuests.OnClientEvent:Connect(function(currentTime, lastClaim)
	currentOSTime = currentTime
	lastOSClaim = lastClaim
end)

while wait(1) do
	script.Parent.Countdown.Text = toHMS(((os.time()) - lastOSClaim))
end

Here’s what it displays on my textlabel: image

This basically shows how much time has passed since the last time the daily quest reset, but I want it to count down from 24:00:00, but I’m not really sure how.

Something like this:

local originalTime = os.time() + 86400

local function toHMS(s) --convert to hours, minutes, seconds and format to 0 left-padded integers separated by colons. 
	return ("%02i:%02i:%02i"):format(s/3600, s/60%60, s%60)
end

local lastReset = os.time() - (60 * 60 * 3) -- 3 hrs ago

while wait(1) do
	print(toHMS( lastReset - os.time() + (60 * 60 * 24) )) -- lastReset - currentTime + 24 hours
end

image

Hi!

Your script is written pretty well, but you can even further improve it by reducing number of calculations, and removing abstraction by not calling the same function every second for such a simple return - no need to add it to call stack again and again continuously when we don’t need to.

We can reverse the process this way:

local FULL_CYCLE = 3600 *24 -- Full day in seconds.

local label = script.Parent.Countdown
local lastClaim = 0

local function questCountDown(lastClaim)
	local current = 0
	repeat
		current = FULL_CYCLE - (os.time() - lastClaim)
		label.Text = ("%02i:%02i:%02i"):format(current/3600, current/60%60, current%60)
		wait(1)
	until current <= 0
	-- Free to start the quest again!
end

PlrDailyQuests.OnClientEvent:Connect(function(lastClaim)
	questCountDown(lastClaim)
end)

EDIT I only realized later than @heII_ish already gave you a similar answer. Both of you guys have a good time!

1 Like

Wow, this worked like a charm! The coding advice really helped as well, I will use this to help optimize my other codes. Also, thank you @hell_ish for your answer!