Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this countdown timer only working in test mode?

I'm making a game where you have a certain amount of time to destroy a wall. This script is basically acting as a countdown timer. I'm using another script inside a gui to get the time information so the player can see. Its supposed to loop and start back at 10:00 after is restarts. (I have another script that makes the wall regenerate when the value of "Clock" is "Restarting...") This all works perfectly in test mode but when I actually play the game it just keeps restarting. Someone help plz lol

TimeInSeconds = 600
TimeInMinutes = math.floor(TimeInSeconds/60)
remainder = TimeInSeconds % 60
clockText = script.Parent.Clock

while wait() do
for i = 1, remainder do
	wait(1)
	if TimeInMinutes > 9 then
		if remainder - i < 10 then
			clockText.Value = TimeInMinutes..":0"..remainder-i..""
		else
			clockText.Value = TimeInMinutes..":"..remainder-i..""
		end
	else
		if remainder - i < 10 then
			clockText.Value = "0"..TimeInMinutes..":0"..remainder-i..""
		else
			clockText.Value = "0"..TimeInMinutes..":"..remainder-i..""
		end
	end
end


for i = 1, TimeInMinutes do
	TimeInMinutes = TimeInMinutes - 1
	for i = 1, 60 do
		wait(1)
		if TimeInMinutes > 9 then
			if 60 - i < 10 then
				clockText.Value = TimeInMinutes..":".."0"..(60-i)..""
			else
				clockText.Value = TimeInMinutes..":"..(60-i)..""
			end
		else
			if 60 - i < 10 then
				clockText.Value = "0"..TimeInMinutes..":0"..(60-i)..""
			else
				clockText.Value = "0"..TimeInMinutes..":"..(60-i)..""
			end
		end
	end
end
clockText.Value = "Restarting..."
wait(2)
end

1 answer

✓ Accepted answer

This is because you tamper with the TimeInMinutes variable. It reaches 0 and isn’t replenished, leaving all conditions unable to proceed thus exiting all for scopes and just calling your print-line.

Not only that, but this code is extremely inefficient, and can be boiled down from forty-six to eleven lines through a much simpler approach. Using string formatting, we can just feed the minutes (i/60) and the seconds (i%60). This doesn’t even require conditions.

Unfortunately, the format characters cannot be recognized by Lua Code Blocks, so I’ve had to make your program here

Hope this helps! If so, don’t forget to accept this answer!

1 comment

0

Thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.