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)
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.
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.
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)