You can write your topic however you want, but you need to answer these questions:
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.
What is the issue? Include screenshots / videos if possible!
With the script I have I don’t know what to do.
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.
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.
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
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.
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.