How would you stop and reset this timer?

I want to stop the timer and reset it once a player touches a brick. All I need to do is create a function that does this, but I just don’t know how.

Timer code:

local runservice = game:GetService("RunService")

local sec = 1
local min = sec * 60

local starttime

local player = game.Players.LocalPlayer
local Starttimer = game.ReplicatedStorage.StartTimer
local Stoptimer = game.ReplicatedStorage.GetTimeAndStop

local function gettime()
	return os.clock()
end

local function readabletime(timer)
	return string.format("%02d:%05.2f", timer/min, timer%min)
end

local function updatetimer()
	local currenttime = gettime()
	local timer = currenttime - starttime
	local timertext = readabletime(timer)
	
	script.Parent.Text = timertext
end

local function starttimer(noreset)
	if noreset then
		if starttime and starttime ~= 0 then
			return
		end
	end
	
	local currenttime = gettime()
	if starttime ~= currenttime then
		starttime = currenttime
		
		coroutine.wrap(function()
			local timertime = starttime
			while timertime == starttime do
				updatetimer()
				runservice.Heartbeat:Wait()
			end
		end)()
	end
end

local function stoptimerandgettime()
	--Where I need to stop, get the player time, and reset the timer
end

Starttimer.OnClientInvoke = starttimer
Stoptimer.OnClientInvoke = stoptimerandgettime

Edit: I had a similar topic like this, but the solutions I got were with modules and other scripts. I want to know how I can stop, record the player’s time, and reset the timer with my current script. Thank you!

1 Like

You can use tick() to check how much time has passed.

local player = game.Players.LocalPlayer

local startTime
local stopTime
local timeBeforePause = 0 

local isRunning = false 
local isPaused = false


function getTime(seconds)
	seconds = tonumber(seconds)

	local hours = string.format("%02.f", math.floor(seconds/3600));
	local mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
	local secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins *60));
	
	return seconds <= 0 and "00:00:00" or hours..":"..mins..":"..secs
end


function updateText()
	local timeInSeconds
	
	while true do
		if not isPaused then
			timeInSeconds = (tick()-startTime)+timeBeforePause	
			script.Parent.Text = getTime(timeInSeconds)
		end
		wait()
	end
end

function startTimer()
	startTime = tick()
	
	if not isPaused then
		startTime = tick()
		coroutine.wrap(updateText)()
	end
	
	isPaused = false
end

function pauseTimer()
	assert(startTime, "You need to start the stopwatch before pausing it")
	
	isPaused = true
	timeBeforePause = timeBeforePause + (tick() - startTime)
end

function stopTimerAndGetTime()
	assert(startTime, "You need to start the stopwatch before stopping it")
	timeBeforePause = 0
	local timeInSeconds = (tick()-startTime)+timeBeforePause
	
	
	startTime = nil
	stopTime = nil
	timeBeforePause = 0 
	isRunning = false 
	isPaused = false
	
	return getTime(timeInSeconds)
end

Also, stopping it your way:

local runservice = game:GetService("RunService")

local sec = 1
local min = sec * 60

local starttime

local player = game.Players.LocalPlayer
local timerEnabled = false

local function gettime()
	return os.clock()
end

local function readabletime(timer)
	return string.format("%02d:%05.2f", timer/min, timer%min)
end

local function updatetimer()
	local currenttime = gettime()
	local timer = currenttime - starttime
	local timertext = readabletime(timer)
	
	script.Parent.Text = timertext
end

local function starttimer(noreset)
	timerEnabled = true
	if noreset then
		if starttime and starttime ~= 0 then
			return
		end
	end
	
	local currenttime = gettime()
	if starttime ~= currenttime then
		starttime = currenttime
		
		coroutine.wrap(function()
			local timertime = starttime
			while timerEnabled and timertime == starttime do
				updatetimer()
				runservice.Heartbeat:Wait()
			end
		end)()
	end
end

local function stoptimerandgettime()
	timerEnabled = false 
	local currenttime = gettime()
	local timer = currenttime - starttime
	local timertext = readabletime(timer)
	print(timertext) 
	--Where I need to stop, get the player time, and reset the timer
end

starttimer()
wait(2)
stoptimerandgettime()
1 Like