So, I am still pretty confused on how to stop the timer. I put these into functions. Can you please explain to me a bit more?
local start = os.clock()
local player = game.Players.LocalPlayer
function startTimer()
while true do
wait()
local diff = os.clock - 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)
script.Parent.Text = min..":"..sec..":"..mil
end
end
function stoptimer()
end
You could have a boolValue somewhere in the explorer, and have the while loop for the timer be dependent on it being true. This would make it so that when you change it to false, the timer will stop. And to do this, having it in the explorer somewhere will allow you to use .Changed, which simply detects when a value changes and does whatever code is within the function.
Here:
local start = os.clock()
local player = game.Players.LocalPlayer
local activatedBool = -- Wherever you put the boolValue in your Explorer
activatedBool.Changed:Connect(function()
while activatedBool == true do
wait()
local diff = os.clock - 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)
script.Parent.Text = min..":"..sec..":"..mil
end
end)
function startTimer()
activatedBool = true
end
function stoptimer()
activatedBool = false
end
Hello to all, I have been watching this topic with interest as I am trying to create the exact same timer.
a timer which is started/activated by an event/touching a block, and ended/finished by touching another block.
I am not a scripter so please excuse me if these are ‘simple issues’. I am not trying to pass this off as my own. Any extra code has been shoehorned in from other peoples work.
Thanks for the above code this has brought me almost to my goal.
I am having a slight problem though. The time works great but it starts from the moment you ‘Spawn in’
I have tried moving the ‘’‘‘local start = os.clock()’’‘’ variable buthavent found the right place yet.
If I wanted to save player final time to a data store would I use ‘’‘‘finalTime’’‘’ variable would it work
the project is a speed-run so lowest time is best.
''''local start = os.clock()
local player = game.Players.LocalPlayer
local label = script.Parent
local activatedBool = game.StarterPlayer.StarterPlayerScripts.Bv:FindFirstChild("BoolValue")
activatedBool.Value = false
startblock = game.Workspace.Start
stopblock = game.Workspace.Stop
function startTimer()
activatedBool.Value = true
end
function stopTimer()
activatedBool.Value = false
end
activatedBool.Changed:Connect(function()
while activatedBool.Value == true do
if not activatedBool then
break
end
wait()
local endTime = 0 --os.clock/tick()
local diff = os.clock() - 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)
if sec <10 then
sec = "0"..sec
end
local finalTime = (string.format("%0.1f", tostring(diff)))
--label.Text = (finalTime)
label.Text = (min.. ":".. sec)--Displays correctly
end
end)
startblock.Touched:Connect(startTimer)
stopblock.Touched:Connect(stopTimer)''''
Use tick(), count the time in seconds then convert it to minutes and hours later
local PlayerTime = 0
local StartTime = tick()
local function startTimer()
StartTime = tick()
end
local function endTimer()
PlayerTime = PlayerTime + (tick() - StartTime)
end
startTimer()
wait(15)
endTimer()
This should get you a time counter of 15 seconds, if you want to make it visible for other clients then use number values and keep updating them
Hello everybody! Thanks to the help of @Hexcede, a fully functional stopwatch timer has been born. For those of you who are having trouble with making a stopwatch, here is a script that will make your worries vanish. Here you go!
Local script inside texlabel:
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
return script.Parent.Text
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()
player.Character.Humanoid.Health = 0
return script.Parent.Text
end
Starttimer.OnClientInvoke = starttimer
Stoptimer.OnClientInvoke = stoptimerandgettime
In the script, there are two remote functions that you can call from other scripts. In order to start the timer, you will have to fire the starttimer remote function with a player parameter. By killing the player, you can automatically stop and reset the timer. I hope this helps those who are trying to make a stopwatch!
local Player = game.Players.LocalPlayer
wait(Player.CharacterAppearanceLoaded)
while true do
wait(1)
local AllPlayTime = Player.AllPlayTime.Value --here only seconds and Int/Number/SringValue (for string use tonubmer())
local minutes = math.floor(AllPlayTime/60)
local seconds = math.floor(AllPlayTime%60)
local hour = math.floor(AllPlayTime/3600)
local days = math.floor(AllPlayTime/86400)
script.Parent.Text = "APT: " ..days.. ":" ..hour%24 .. ":" ..minutes%60 .. ":" .. seconds%60 .. ""
end
--remove any value if u don't need use him (Example: script.Parent.Text = "APT: " ..days.. ":" ..hour%24 .. ":" ..minutes%60 .. ""