Timer not stoping

Hey Devs,

I have a problem with my script: it’s supposed to start when you touch a part, and stop when you touche another, although it starts and stop immediately… The timer works fine until I add the break and the if/else that comes before it.

Thanks so much :slight_smile:

local Players = game.Players
local player = Players.LocalPlayer

local minutes = 0
local seconds = 0
local miliseconds = 0

game.Workspace.Timer.Touched:Connect(function()
	
	if minutes ~= 0 or seconds ~= 0 or miliseconds ~= 0 then
		minutes = 0
		seconds = 0
		miliseconds = 0
	end
	
	while true do

		script.Parent.Text = minutes..":"..seconds..":"..miliseconds
		
		miliseconds = miliseconds + 1
		
		if miliseconds == 1000 then
			miliseconds = 0
			seconds = seconds + 1
		end
		
		if seconds == 60 then
			seconds = 0
			minutes = minutes + 1
		end
		
		wait(0.001)
		
		if game.Workspace.Stop.Touched then
			print("Stopped")
			break
		end
		
	end
	
end)
1 Like

i think the problem is the if statement (i may be wrong)

local Players = game.Players
local player = Players.LocalPlayer

local minutes = 0
local seconds = 0
local miliseconds = 0

game.Workspace.Timer.Touched:Connect(function()
	
	if minutes ~= 0 or seconds ~= 0 or miliseconds ~= 0 then
		minutes = 0
		seconds = 0
		miliseconds = 0
	end
	
	while true do

		script.Parent.Text = minutes..":"..seconds..":"..miliseconds
		
		miliseconds = miliseconds + 1
		
		if miliseconds == 1000 then
			miliseconds = 0
			seconds = seconds + 1
		end
		
		if seconds == 60 then
			seconds = 0
			minutes = minutes + 1
		end
		
		wait(0.001)
		
		game.Workspace.Stop.Touched:Connect(function()
			print("Stopped")
			break
		end)
		
	end
	
end)
2 Likes

I think that that is definitely a good idea, although the break is red and an error says that break must be inside a loop. I think putting it in a function makes it not be in the while loop somehow…

Hey! I just want to point that using wait(0.001) in while true do is not a good practice and may affect your performance!

There are methods which can be as x2 faster than using that,
I recommend seeing this topic: Avoiding wait() and why, It’ll definetly help in future scripts.

1 Like

Hey, thanks for the heads up :slight_smile: How could I implement what is being proposed in the article in my script? I feel like since this is a timer it pretty much needs to have a wait(n)…

You could set up a Touched event for the stop button outside of the while loop, then set a variable hasStopped = true, or something of that sort (make sure to check if the player touched it, and not some random part). Once the stop button has been pressed, disconnect the event. Then in the loop, you can check if hasStopped is true, and break the loop.

2 Likes

Thank you so much, that worked. I can now go to sleep knowing I fixed that glitch lol :smiley:

Yeah, as I said, I recommend reading that article as it explains most of those questions.

1 Like

Thank you very much, this is probably why my timer wasn’t very accurate and was kind of slow. :smiley:

1 Like