I have a little problem with the timer resetting for everyone whenever someone walks through the “starttimer” block. The timer works flawlessly though but that’s the only problem I ran into! I tried tampering with the “onStartTouched” function and local lines at the top, but I ruined the whole thing when I tested it, so I changed everything back to what it is below! ![]()
local startTime = 0
local timerStopped = true
local Runservice = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local personalBest = player:WaitForChild("PersonalBest")
local replicatedStorage = game:GetService("ReplicatedStorage")
local function secondsToMinSecMs(seconds)
local minutes = math.floor(seconds / 60)
local remainingSeconds = seconds % 60
local secondsSplit = math.floor(remainingSeconds)
local millisecondsSplit = math.floor((remainingSeconds - secondsSplit) * 100)
return string.format("%02i:%02i.%02i", minutes, secondsSplit, millisecondsSplit)
end
local textLabel = script.Parent
textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)
personalBest.Changed:Connect(function()
personalBest.Value = math.clamp(personalBest.Value, 1/math.huge, math.huge)
-- if number is less than the smallest positive number possible (x <= 0)
-- then the line above will make number the smallest positive number possible (x ≈ 0, x > 0)
-- peventing personal best to be negative numbers or exactly 0
textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)
end)
local function onStartTouched()
currentTime = 0
startTime = tick()
timerStopped = false
end
local function onEndTouched()
timerStopped = true
if currentTime < personalBest.Value then
personalBest.Value = currentTime
replicatedStorage.BestTime:FireServer(currentTime)
end
textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)
end
Runservice.RenderStepped:Connect(function()
if not timerStopped then
currentTime = tick() - startTime
textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)
end
end)
workspace:WaitForChild("StartTimer").Touched:Connect(onStartTouched)
workspace:WaitForChild("EndTimer").Touched:Connect(onEndTouched)