How to Reset this timer?

im trying to reset this timer when a specific part is touched.

local localplayer = game.Players.LocalPlayer
local gui = localplayer.PlayerGui:FindFirstChild("Timer"):FindFirstChild("TextLabel")
local run = game:GetService("RunService")

local function starttimer(starttime)
	local Timer = run.Heartbeat:Connect(function()
		local Elapsed = os.clock(starttime) - starttime
		local Minutes = math.floor(Elapsed / 60)
		local Seconds = math.floor(Elapsed % 60)
		local Milliseconds = (Elapsed % 1) * 100
		gui.Text = string.format("%.2d:%.2d.%.2d", Minutes, Seconds, Milliseconds)
	end)
end

game.ReplicatedStorage.ChangeTimer.OnClientEvent:Connect(function()
	localplayer.PlayerGui:FindFirstChild("ObbyName"):FindFirstChild("TextLabel").Text = "Blue"
	localplayer.PlayerGui:FindFirstChild("ObbyName"):FindFirstChild("TextLabel").TextColor3 = Color3.new(0, 0.0509804, 1)
	local starttime = os.clock(0)
	starttimer(starttime)
end)

i tried doing textlabel.Text = “00:00.00” but it doesnt reset

4 Likes

Returns the Heartbeat connection in your starttimer function then simply disconnect your event by doing this:

local localplayer = game.Players.LocalPlayer
local gui = localplayer.PlayerGui:FindFirstChild("Timer"):FindFirstChild("TextLabel")
local run = game:GetService("RunService")

local function starttimer(starttime)
	local Timer = run.Heartbeat:Connect(function()
		local Elapsed = os.clock(starttime) - starttime
		local Minutes = math.floor(Elapsed / 60)
		local Seconds = math.floor(Elapsed % 60)
		local Milliseconds = (Elapsed % 1) * 100
		gui.Text = string.format("%.2d:%.2d.%.2d", Minutes, Seconds, Milliseconds)
	end)

    return Timer -- returns the connection
end

game.ReplicatedStorage.ChangeTimer.OnClientEvent:Connect(function()
	localplayer.PlayerGui:FindFirstChild("ObbyName"):FindFirstChild("TextLabel").Text = "Blue"
	localplayer.PlayerGui:FindFirstChild("ObbyName"):FindFirstChild("TextLabel").TextColor3 = Color3.new(0, 0.0509804, 1)
	local starttime = os.clock(0)
	local timerEvent = starttimer(starttime)

    timerEvent:Disconnect() -- Stop the timer then set your text to 00:00.00
end)

so i added another remoteevent called resettimer

local localplayer = game.Players.LocalPlayer
local gui = localplayer.PlayerGui:FindFirstChild("Timer"):FindFirstChild("TextLabel")
local run = game:GetService("RunService")

local function starttimer(starttime)
	local Timer = run.Heartbeat:Connect(function()
		local Elapsed = os.clock(starttime) - starttime
		local Minutes = math.floor(Elapsed / 60)
		local Seconds = math.floor(Elapsed % 60)
		local Milliseconds = (Elapsed % 1) * 100
		gui.Text = string.format("%.2d:%.2d.%.2d", Minutes, Seconds, Milliseconds)
	end)
	return Timer
end

game.ReplicatedStorage.ChangeTimer.OnClientEvent:Connect(function()
	localplayer.PlayerGui:FindFirstChild("ObbyName"):FindFirstChild("TextLabel").Text = "Blue"
	localplayer.PlayerGui:FindFirstChild("ObbyName"):FindFirstChild("TextLabel").TextColor3 = Color3.new(0, 0.0509804, 1)
	local starttime = os.clock(0)
	starttimer(starttime)
end)

game.ReplicatedStorage.ResetTimer.OnClientEvent:Connect(function()
	local starttime = os.clock(0)
	local timerEvent = starttimer(starttime)
	timerEvent:Disconnect()
	gui.Text = "00:00.00"
end)

is this what im supposed to do? bc it still doesnt work

Here you create a new timer, the objective of disconnecting your first timer, here is the corrected code:

local localplayer = game.Players.LocalPlayer
local gui = localplayer.PlayerGui:FindFirstChild("Timer"):FindFirstChild("TextLabel")
local run = game:GetService("RunService")

local function starttimer(starttime)
	local Timer = run.Heartbeat:Connect(function()
		local Elapsed = os.clock(starttime) - starttime
		local Minutes = math.floor(Elapsed / 60)
		local Seconds = math.floor(Elapsed % 60)
		local Milliseconds = (Elapsed % 1) * 100
		gui.Text = string.format("%.2d:%.2d.%.2d", Minutes, Seconds, Milliseconds)
	end)
	return Timer
end

local currentTimer -- Look at this line
game.ReplicatedStorage.ChangeTimer.OnClientEvent:Connect(function()
	localplayer.PlayerGui:FindFirstChild("ObbyName"):FindFirstChild("TextLabel").Text = "Blue"
	localplayer.PlayerGui:FindFirstChild("ObbyName"):FindFirstChild("TextLabel").TextColor3 = Color3.new(0, 0.0509804, 1)

	local starttime = os.clock(0)
	currentTimer  = starttimer(starttime) -- Look at this line
end)

game.ReplicatedStorage.ResetTimer.OnClientEvent:Connect(function()
	currentTimer:Disconnect() -- Look at this line
	gui.Text = "00:00.00"
end)```

Try to see the difference, I don't have more time to explain to you immediately, if you don't understand, say so, me or someone else will answer you later.

The main difference is that we use the same variable for your two events: “currentTimer”

RunService.Heartbeat is.
The function doesn’t return os.clock but the RBXScriptConnection given by RunService.Heartbeat.

Ohhh, I think I see it now, mb.

However the if statement is a good improvement cause in my code I didn’t check if a timer has been started, creating an error when we reset the timer before creating it.

this is my current code

local localplayer = game.Players.LocalPlayer
local gui = localplayer.PlayerGui:FindFirstChild("Timer"):FindFirstChild("TextLabel")
local run = game:GetService("RunService")

local function starttimer(starttime)
	local Timer = run.Heartbeat:Connect(function()
		local Elapsed = os.clock(starttime) - starttime
		local Minutes = math.floor(Elapsed / 60)
		local Seconds = math.floor(Elapsed % 60)
		local Milliseconds = (Elapsed % 1) * 100
		gui.Text = string.format("%.2d:%.2d.%.2d", Minutes, Seconds, Milliseconds)
	end)
	return Timer
end

local currentTimer

game.ReplicatedStorage.ChangeTimer.OnClientEvent:Connect(function()
	localplayer.PlayerGui:FindFirstChild("ObbyName"):FindFirstChild("TextLabel").Text = "Blue"
	localplayer.PlayerGui:FindFirstChild("ObbyName"):FindFirstChild("TextLabel").TextColor3 = Color3.new(0, 0.0509804, 1)
	local starttime = os.clock(0)
	currentTimer = starttimer(starttime)
end)

game.ReplicatedStorage.ResetTimer.OnClientEvent:Connect(function()
	currentTimer:Disconnect()
	gui.Text = "00:00.00"
end)

am i doing something wrong here? cuz its still not working

Alright, I think I got it.

Try this script out (Client Sided, inside of the timerGui):

local localplayer = game.Players.LocalPlayer
local gui = localplayer.PlayerGui:FindFirstChild("Timer"):FindFirstChild("TextLabel")
local run = game:GetService("RunService")

local Timer

local function starttimer(starttime)
    Timer = run.Heartbeat:Connect(function()
		local Elapsed = os.clock(starttime) - starttime
		local Minutes = math.floor(Elapsed / 60)
		local Seconds = math.floor(Elapsed % 60)
		local Milliseconds = (Elapsed % 1) * 100
		gui.Text = string.format("%.2d:%.2d.%.2d", Minutes, Seconds, Milliseconds)
	end)
end

starttimer(1)

game.ReplicatedStorage.ChangeTimer.OnClientEvent:Connect(function()
	local starttime = os.clock(0)
	starttimer(starttime) 
end)

game.ReplicatedStorage.ResetTimer.OnClientEvent:Connect(function()
	if typeof(Timer) == "RBXScriptConnection" then
		if Timer.Connected then
			Timer:Disconnect()
			Timer = nil
			
			print("disconnected")
			gui.Text = "00:00.00"
		end
	end
end)

Then, this is the script to reset the timer
(Server Script, inside of ServerScriptService):

local t = game:GetService("ReplicatedStorage").ResetTimer
local globalplayer

game.Players.PlayerAdded:Connect(function(plr)
	task.wait(5)

	print("reset")
	t:FireClient(plr)
end)

Just tried it out in studio and its working fine.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.