How to decrease hour:min:second formated string

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    so, I want to decrease the Reward Cooldowns while using hour,min,second format but it doesn’t work while using the hour:min:sec format
    here is a video of what I mean


    last reward’s time doesn’t decrease but others do

  2. What is the issue? Include screenshots / videos if possible!
    I got this error
    attempt to compare number < nil
    I understat that error occures because hour:min:second format is a string type not number but I don’t know how to fix it

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local function formatTime(s)
	return string.format("%02i:%02i:%02i", s/60^2, s/60%60,s%60)
end

local function TickTime(v)
	v.Text = DailyRewardsModule[v.Name]["ClaimTime"]
	while tonumber(v.Text) >1  do
		print(v.Text)
		if tonumber(v.Text) > 60 then
			v.Text = formatTime(v.Text - 1)
		
		else v.Text = v.Text -1 
	 end
		print(v.Text)
		task.wait(1)
	end
	if tonumber(v.Text) == 1 then
		print("timeReached0")
		v.Text = "Redeem"
	end
end


for i , v in pairs(Frame:GetDescendants()) do
	if v:IsA("TextLabel")  then
		coroutine.wrap(TickTime)(v)
	end
end

Yeah I still wonder the answer.

Tell me if this fixes it and if you want an explanation.

local function formatTime(s)
	return string.format("%02i:%02i:%02i", s/60^2, s/60%60,s%60)
end

local function TickTime(v)
	v.Text = DailyRewardsModule[v.Name]."ClaimTIme"
	while tonumber(v.Text) > 1  do
		print(v.Text)
		if tonumber(v.Text) > 60 then
			v.Text = formatTime(v.Text - 1)
		else v.Text = tonumber(v.Text) -1 
	 end
		print(v.Text)
		task.wait(1)
	end
	if tonumber(v.Text) == 1 then
		print("timeReached0")
		v.Text = "Redeem"
	end
end


for i , v in pairs(Frame:GetDescendants()) do
	if v:IsA("TextLabel")  then
		coroutine.wrap(TickTime)(v)
	end
end

I just had chance to text it and sadly no. It didn’t fixed
just want to say if there is a misunderstatement I am trying to decrease the hour:min:sec format
I posted the video there
the numbers decrease normally but when they are changed to format(like last reward) it doesn’t decrease
probably because it is not a number anymore

What you can do is calculate the time remaining for the reward to be available then use

local TimeTilAvailable: string = os.date("!%X", Seconds)
-- formatted in HH:MM:SS
1 Like
  1. The DailyRewardsModule[v.Name]["ClaimTime"] value is not being updated: Make sure that this value is being updated somewhere in your code, so that the timer has a value to count down from.
  2. The formatTime function is returning a string, which needs to be converted back to a number before subtracting 1: In the line v.Text = formatTime(v.Text - 1), you need to convert the result of formatTime back to a number using tonumber before subtracting 1. Otherwise, you will be subtracting 1 from a string, which will not work as expected.

Here’s the updated code with these changes:

local function formatTime(s)
	return string.format("%02i:%02i:%02i", s/60^2, s/60%60,s%60)
end

local function TickTime(v)
	v.Text = DailyRewardsModule[v.Name]["ClaimTime"]
	while tonumber(v.Text) > 1 do
		print(v.Text)
		if tonumber(v.Text) > 60 then
			v.Text = formatTime(tonumber(v.Text) - 1)
		else 
			v.Text = tonumber(v.Text) - 1 
		end
		print(v.Text)
		task.wait(1)
	end
	if tonumber(v.Text) == 1 then
		print("timeReached0")
		v.Text = "Redeem"
	end
end


for i , v in pairs(Frame:GetDescendants()) do
	if v:IsA("TextLabel")  then
		coroutine.wrap(TickTime)(v)
	end
end

This should update the timer correctly and countdown the time in the format hh:mm:ss.

nope the problem is here while tonumber(v.Text) > 1 do since it becomes a string it will no more do tonumber (v.Text) > 1
and no, it didn’t fixed
this is the error I am still getting attempt to compare number < nil at while line

I am not sure about how to use this in script

Set the .Text value of the TextLabels that you are using to the string value

Okay firsly I apologize but I am really stupid and I Don’t really understand what do you mean by

do you mean I should Change the value in textlabels’s properties manually
or do you mean like this

local function TickTime(v)
	local TimeTilAvailable: string = os.date("!%X", seconds)
	v.Text = DailyRewardsModule[v.Name]["ClaimTime"]

	while tonumber(v.Text) > 1 do

		if tonumber (v.Text) > 60 then
			v.Text = TimeTilAvailable(v.Text - 1)
		else 
			v.Text = tonumber(v.Text) - 1 
		end
		task.wait(1)
	end

	if tonumber(v.Text) == 1 then
		print("timeReached0")
		v.Text = "Redeem"

	end
end

But it willl say “seconds” unknown global
or did you mean

local function formatTime(seconds)

local TimeTilAvailable: string = os.date("!%X", seconds)

end

but this time it returns this error at coroutine.wrap(TickTime)(v) line
Unable to assign property Text. string expected, got nil
sorry I just don’t really understand that

do not think of yourself this way, everyone starts somewhere


You should be changing the value of the TextLabels, just not in the way that you are doing right now
while loops are very inefficient and will break the instant that an error occurs

Instead of using a while loop, you should be using something else such as RunService.Heartbeat to update the timers

You also need to figure out a way to get the amount of time until the player is able to claim the reward, which is what you input into os.date("!%X", seconds)

I used this to get the time like 1st reward is 10 seconds, 2nd reward is 20 second
and if I don’t use format time it works perfectly but after I changed text to format it started to give error

even if ClaimTime is a number somehow it says it is a string sp I have to use ToNumber to turn it a number
But FormatTime Turns that time to a string, so while loop won’t work for that reward because it is not a number anymore…

Yeah… I felt that but It seems really confusing

Thanks!

This maybe?

local time = "1:11:11" -- 4,271 seconds

local function getSecondsFromTime(time: string): number
	local splits = time:split(":")
	local seconds = 0
	
	for i = 2, 0, -1 do
		seconds += tonumber(splits[3 - i]) * 60^i
	end
	
	return seconds
end

print(getSecondsFromTime(time)) -- 4271

That seems worty to try but I am not sure if I can make it work with script it looks more confusing to me rightnow.
well, I simply tried this but it returned error
attempt to index function with 'split'

local function formatTime(seconds)
	
		return string.format("%02i:%02i:%02i", seconds/60^2, seconds/60%60,seconds%60)
end

local function getSecondsFromTime(time: string): number
	local splits = time:split(":") -- Error Shows here
	local seconds = 0

	for i = 2, 0, -1 do
		seconds += tonumber(splits[3 - i]) * 60^i
	end

	return seconds
end
print(getSecondsFromTime(time))


local function TickTime(v)
	v.Text = DailyRewardsModule[v.Name]["ClaimTime"]
	print("welp")
	while tonumber(v.Text) > 1 do
		
		if tonumber (v.Text) > 60 then
			print("Test2")
			print(v.Text)
			v.Text = formatTime(v.Text - 1)
			getSecondsFromTime(v.Text)
			print(v.Text)
		else 
			v.Text = tonumber(v.Text) - 1 
		end
		task.wait(1)
	end
	if tonumber(v.Text) == 1 then
		print("timeReached0")
		v.Text = "Redeem"
	end
end


for i , v in pairs(Frame:GetDescendants()) do
	if v:IsA("TextLabel")  then
		coroutine.wrap(TickTime)(v)
	end
end

The error occurs here:

print(getSecondsFromTime(time))

Not the line you marked. The reason is because that line used a variable that I called “time” which is a function, but I override it. Remove that print and it’ll work.

Yep! function works fine but I still got the same error at same line

That means the v.Text can’t be a number, which makes sense, you need to call the function I gave you instead of the tonumber.

That won’t do because to get ClaimTime from module as number I have to use tonumber

v.Text = DailyRewardsModule[v.Name]["ClaimTime"]
	print("welp")
	while tonumber(v.Text) > 1 do

tonumber won’t work in this case, that’s why my function exists, it will return the correct number in seconds.

I think I couldn’t explain it
v.Text can be a number
That “ClaimTime” is a number not a hh:mm:ss format


so I can’t use get seconds…
did you mean that?
or did you mean like this?

local function TickTime(v)
	v.Text = DailyRewardsModule[v.Name]["ClaimTime"]
	print("welp")
	while toNumber(v.Text) > 1 do
		
		if tonumber (v.Text) > 60 then
			print("Test2")
			print(v.Text)
			v.Text = formatTime(v.Text - 1)
			v.Text=getSecondsFromTime(v.Text)
			print(v.Text)
		else 
			v.Text = tonumber(v.Text) - 1 
		end
		task.wait(1)
	end
	if tonumber(v.Text) == 1 then
		print("timeReached0")
		v.Text = "Redeem"
	end
end

if so it won’t return the format
or did I misunderstand you