How can I stop and record the time of my timer when a player steps on a block?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a block that will stop and record the time of a timer I made when a player finishes an obby.
  2. What is the issue? Include screenshots / videos if possible!
    With the script I have I don’t know what to do.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried using a script that will stop the timer but I didn’t understand.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

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)
	local remainder = timer
	local minutes = math.floor(remainder/min)
	remainder = remainder%min
	local seconds = math.floor(remainder/sec)
	remainder = timer%sec
	local hundofsec = math.floor(remainder*10^2)/10^2
	
	minutes = tostring(minutes)
	if #minutes < 2 then
		minutes = "0"..minutes
	end
	
	seconds = tostring(seconds)
	if #seconds < 2 then
		seconds = "0"..seconds
	end
	
	hundofsec = tostring(hundofsec):sub(3,5)
	if #hundofsec < 2 then
		seconds = "0"..seconds
	end
	
	return table.concat({minutes, seconds.."."..hundofsec}, ":")
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()
	starttime = 0
end

Starttimer.OnClientInvoke = starttimer
Stoptimer.OnClientInvoke = stoptimerandgettime

How will I stop, record, and reset the time?

I made a function that is supposed to stop the timer and get the time, but I’ m not sure how to do it. The starttime sets to 0 but it will just run the timer from 0.

3 Likes

Just asking for clarification real quick. Is the timer a gui?

Yes it is. The local script is in a timer.

Hi, I’m not going to help you with your script, but rather give you an alternative.
I did this using the Timer module which can do what you want. I hope it works for you.
Baseplate.rbxl (31.5 KB) UPDATE 2
Note: I used the readabletime function of your script. It seems to fail to format the time. You can look for an alternative in the forum.

2 Likes

I always wondered why the text is weird. Do you think it’s a problem with the script or roblox itself?

I debugged

Correct
  in     out
  1.21   00:01.21
  1.22   00:01.22
  2.05   00:02.04
  2.06   00:02.05
incorrect
  in     out
  1      00:001.
  1.1    00:001.1
  1.2    00:001.2
  1.5    00:001.5
  2      00:002.

but it is easier to use the string.format function

string.format("%02d:%05.2f", _time/60, _time%60)

here is the tutorial

1 Like

Thank you! But because I am new to this concept, what would I replace in my script with string.format?

your readabletime function would look like this

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

Wow thank you so much! The timer now runs very smoothly!

Back to the issue though. How would I record the time in my function? I want to make a system message that says the player’s name has beaten what tower in their time. Would I just get the time by saying script.Parent.Text, kill the player so their time resets, and then make the message?

I have updated the place. I have added a function called onTimerStop in which you can put the logic you want. Also I have modified a little bit the Timer module

1 Like

Here’s a simple fix. Wrap the logic behind your timer in a while loop, that says something like this:

local timerRunning = true

while timerRunning == true do
    if --[[the player dies or whatever--]] then
        timerRunning = false
    end
-- Your timer here
end

This should effectively stop your timer when timerRunning is switched to false.

1 Like

thanks for this! I have a couple questions!, sorry made a dumb mistake and fixed the visible.

was wondering how can I pair this with a leaderboard on a part? to show all the fastest players?

there are many ways to do this. I updated the place with some modifications. This will give you an idea of how to achieve it. I hope this helps you

1 Like

I do see what you did! thanks! but this is a remote, would I have to set up the datastore and attach it with the script linked to button?

local allReordsEvent = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("AllRecords")

local playerRecord: RemoteEvent = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("PlayerRecord")

allReordsEvent.OnClientEvent:Connect(function (playersRecords)

-- all players Records

for player, record in pairs(playersRecords) do

print(player, record)

end

end)```

that button is just to see the records of all the players. That event could be fired every time someone has a record so that your leaderboard is updated (assuming your leaderboard is on the client side. As I said there are many ways to do this).

To save the data you would have to save the playersrecords table in a Datastore from time to time and when the player goes.

can you tell me where in your script how I could turn off resetting the clock after death

1 Like

every time the player dies, the whole script is destroyed and when the player character spawns the script starts running from scratch. so with this script there is no specific place for that.
A solution would be to move the script to StarterPlayerScripts and change some of the logic, of course.

1 Like