If statement seems to stop a `while` loop

This is the loop I have:

while true do
	print(1)
	if Seconds > 9 and Milliseconds > 9 then
		TimeRemainingText.Text = ("%s : %s"):format(tostring(Seconds,Milliseconds))
		print("case 1")
	elseif Seconds > 9 and Milliseconds < 9 then
		TimeRemainingText.Text = ("%s : 0%s"):format(tostring(Seconds,Milliseconds))
		print("case 2")
	elseif Seconds < 9 and Milliseconds > 9 then
		TimeRemainingText.Text = ("0%s : %s"):format(tostring(Seconds,Milliseconds))
		print("case 3")
	elseif Seconds < 9 and Milliseconds < 9 then
		TimeRemainingText.Text = ("0%s : 0%s"):format(tostring(Seconds,Milliseconds))	
		print("case 4")
	end
	print(2)
	if Milliseconds == 0 then Seconds -= 1 Milliseconds = 99 end
	Milliseconds -= 1
	print(3)
	if Seconds == 0 and Milliseconds == 0 then break end
	print(4)
	task.wait()
	print("reached")
end

And the issue seems to be the if block. If I remove it, it all works, and the loop continues. But if it stays, only “1” prints in the output. What’s going on?

Only thing I can think of is that there is an error somewhere, which maybe you haven’t noticed, which is yielding the entire script. Double check the output, maybe? If that doesn’t work try printing “case 1” and so on before setting the TimeRemainingText and see if that changes anything

Could you try this code?

while true do
    print(1)
    if Seconds > 9 and Milliseconds > 9 then
        TimeRemainingText.Text = ("%s : %s"):format(tostring(Seconds), tostring(Milliseconds))
        print("case 1")
    elseif Seconds > 9 and Milliseconds < 9 then
        TimeRemainingText.Text = ("%s : 0%s"):format(tostring(Seconds), tostring(Milliseconds))
        print("case 2")
    elseif Seconds < 9 and Milliseconds > 9 then
        TimeRemainingText.Text = ("0%s : %s"):format(tostring(Seconds), tostring(Milliseconds))
        print("case 3")
    elseif Seconds < 9 and Milliseconds < 9 then
        TimeRemainingText.Text = ("0%s : 0%s"):format(tostring(Seconds), tostring(Milliseconds))    
        print("case 4")
    end
    print(2)
    if Milliseconds == 0 then
        Seconds = Seconds - 1
        Milliseconds = 99
    end
    Milliseconds = Milliseconds - 1
    print(3)
    if Seconds == 0 and Milliseconds == 0 then break end
    print(4)
    task.wait()
    print("reached")
end

This is all I get:


By placing the prints before the TimeRemainingText.Text setup, this is what I get:

Do you have errors enabled? Maybe it isn’t working because you have an error but you have those disabled.

All I see differently is this line. I might not be seeing any changes you’ve made. The code doesn’t reach the print(2) line, so I don’t see how it could change anything

Tostring format is modified, Did you even try it?

1 Like

Ah, I’m sorry, I didn’t see that part. And looking back, this is how a tostring() should even look like. Let me try the change and see if it works, and by now I could say your code is the solution.

Glad that it worked!
Goodluck with your project :+1:

1 Like

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