Digital Clock Issue

Hello, I am attempting to make a working digital clock, it worked just fine until I added a piece of code.
No error codes in Output,

(I know it’s a bit excessive.)
Script:

local hour
local minute
local extrazero = 0
local timevalue = script.Parent.Parent.Parent.Parent.Time
local pause = script.Parent.Parent.Parent.Parent.Paused

while true do
	wait(5)
-- Issue starts from code below.
	if string.len(timevalue.Value) == 3 then
		hour = string.sub(timevalue.Value, 1,1)
		minute = string.sub(timevalue.Value, 2,3)
	elseif string.len(timevalue.Value) == 4 then
		hour = string.sub(timevalue.Value, 1,2)
		minute = string.sub(timevalue.Value, 3,4)
	end
-- The rest of the code works like it is supposed to.
	if pause.Value == false then
		if hour == 12 then
			if minute == 59 then
				hour = 1
				minute = 00
				script.Parent.Text = (hour .. ":" .. extrazero .. minute)
				timevalue.Value = (hour .. extrazero .. minute)
			else
				if string.len(minute) == 1 and minute ~= 9 then
					minute += 1
					script.Parent.Text = (hour .. ":" .. extrazero .. minute)
					timevalue.Value = (hour .. extrazero .. minute)
				else
					minute += 1
					script.Parent.Text = (hour .. ":" .. minute)
					timevalue.Value = (hour .. minute)
				end
			end
		elseif minute == 59 then
			hour += 1
			minute = 00
			script.Parent.Text = (hour .. ":" .. extrazero .. minute)
			timevalue.Value = (hour .. extrazero .. minute)
		else
			if string.len(minute) == 1 and minute ~= 9 then
				minute += 1
				script.Parent.Text = (hour .. ":" .. extrazero .. minute)
				timevalue.Value = (hour .. extrazero .. minute)
			else
				minute += 1
				script.Parent.Text = (hour .. ":" .. minute)
			timevalue.Value = (hour .. minute)
			end
		end
	end
end

Photo showing the layout:
code-2

The issue is that the minutes keep going, even though it has passed 60 minutes.
Any help would be appreciated!

1 Like

God this code is messy, you can easily simplify the code to:

if Minute >= 60 then
    Hour += 1
end

Hour = (Hour % 12) + 1
Minute %= 60 -- returns to 0 once the number reached 60
string.format("%d:%02i", Hour, Minute) -- Ex: (1:23) (1:02)

Just use the # operator for string.len(), its much faster

Also, can you show whats wrong?

Sorry for the messy code, I’m not the best at coding.
The issue I’m facing with my current code is that the minutes exceed 60 and keep on going.
When I was testing it, I realized it went over 60,
issue
I am trying to be able to set the time easily using the timevalue variable, and also be able to pause with the pause variable.
The issue came from the code that sets the hour and minute.