how do i pause tick() i can’t find any other helpful posts on this topic
local start = tick()
game:GetService("RunService").RenderStepped:Connect(function()
if script.Parent.Parent.Paused.Value == false then
local diff = tick() - start
local min = math.floor(diff / 60)
local sec = diff - min*60
local mil = math.floor((sec - math.floor(sec)) * 100)
sec = math.floor(sec)
script.Parent.Text = min..":"..sec..":"..mil
else
--stop the variable start from ticking
end
end)
What do you mean by “pause?” Do you mean stop the value from updating? start will always have the same value; the only thing that’s changing is the new tick call you’re doing here:
To fix your issue, you can just update the start value to the newest tick value in the else block:
You cannot pause tick(); the value it returns is based on the amount of real life time that has passed since a certain date. This looks like a timer script though, yeah?
RenderStepped passes in how much time has passed since it last fired, and that can be used to track passed time. To pause the timer, all you need to do is disconnect the event listener, like this:
--Every frame, "elapsed_time" will increase by how much time has passed since the last frame
--so long as this is connected to RenderStepped.
local elapsed_time = 0
local function increment_timer(delta_time)
elapsed_time += delta_time
local min = math.floor(elapsed_time / 60)
local sec = elapsed_time - min*60
local mil = math.floor((sec - math.floor(sec)) * 100)
sec = math.floor(sec)
script.Parent.Text = min..":"..sec..":"..mil
end
--To pause the script, listen for changes to the "Paused" BoolValue's Value and connect/disconnect
--the RenderStepped connection to stop it from updating.
local timer_connection
local paused_value = script.Parent.Parent.Paused
local function pause_timer(paused)
if paused and timer_connection then
timer_connection:Disconnect()
timer_connection = nil
elseif not (paused or timer_connection) then
timer_connection = game:GetService("RunService").RenderStepped:Connect(increment_timer)
end
end
paused_value.Changed:Connect(pause_timer)
pause_timer(paused_value.Value)
Heartbeat is tied to physics, RenderStepped is tied to frame drawing. On an unmodified client, these events will both fire once per frame, albeit it at different points. In your use case, either would be fine, but you’ll generally want to use Heartbeat when you don’t need to run logic specific to frame drawing.