Issue with stopwatch for obby

What want to achieve
I want to achieve stopwatch for obby that will turn on when you cross the red line(you can see it on video) and you can turn on it again if you fall.

Issue

One of my best attempts is this. Code that i use:

game.Workspace.Stage1.TimerStart.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then 
	game.Workspace.Stage1.TimerStart.Position = Vector3.new(500,500,500) 
--TimerStart is a part that you need to cross to activate stopwatch
--when you fall TimerStart position is going to be normal and player can activate it again
		local start = tick()

		print(3)
	while true do
		wait() 
			if game.Workspace.Stage1.TimerStart.Position == Vector3.new(500,500,500) then
				
				local diff = tick() - start

		local min = math.floor(diff / 60)
		local sec = diff - min*60
		local mil = math.floor((sec - math.floor(sec)) * 10) 
				
				sec = math.floor(sec)
				
				print(min, sec, mil)

					script.Parent.TextLabel.Text = min..":"..sec..":"..mil

				if game.Workspace.Stage1.TimerStart.Position == Vector3.new(500,500,500) then

		print(min, sec, mil)
				script.Parent.TextLabel.Text = min..":"..sec..":"..mil
				end
			end
		end
	end	
end)

game.Workspace.Stage1.TimerStart.TouchEnded:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then 
		game.Workspace.Stage1.TimerStart.Position = Vector3.new(500,500,500)
	end	
end)

Issue is that stopwatch for first time use works great but when you activate it again it starts to write two numbers: one from old use and one from new use at the same time. And maybe another issue that can mess with script that character touches part too many times (like 3-5 times when c rossing it(i redused part size to minimal and still this problem)) but i dont think that it causes an issue(sometimes i get 1 part touch, sometimes i got 7 part touches and script works good for first time use in both situations)

Define a variable and use this variable for your loop. You’re better off using some arbitrary value for your loop like tick() so you can ensure that only the latest thread remains active:

local timerStart

part.Touched:Connect(function(otherPart)
    local thisTick = tick()
    timerStart = thisTick
    while thisTick == timerStart do
        -- do timer calculations and whatnot here
    end
end)

If you ever need to abruptly stop the thread you can just set timerStart to anything (such as nil), then the currently active loop will stop once it reaches the next iteration

To handle the multiple part touches you can implement a “debounce”.

If you are new to the topic I would recommend this video How to make a Cooldown with Debounce - Roblox Studio - YouTube. Hopefully it explains it well.

Hey! I try use your tip and still didnt work
image
It writes two(but code still one: print(min, sec, mil)) values and I really cant understand why its still remember old value or how to do that old value will just disappear and will be only new value. Do you know how to delete old value? Maybe I should use return or something another?