hello im trying to reset a timer if a player touches a specific part
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)
return Timer
end
game.ReplicatedStorage.ChangeTimer.OnClientEvent:Connect(function()
local starttime = os.clock(0)
starttimer(starttime)
end)
game.ReplicatedStorage.ResetTimer.OnClientEvent:Connect(function()
print(gui.Text)
if typeof(Timer) == "RBXScriptConnection" then
if Timer.Connected then
Timer:Disconnect()
Timer = nil
task.wait()
print("disconnected")
gui.Text = "00:00.00"
end
end
end)
im confused because the timer sometimes resets, and sometimes doesnt. is it because of the :Disconnect()? if so how should i replace it
You don’t have to look for another method, this is completely fine.
I have looked into your scripts and commented errors.
-- I don't think RBXScriptConnections have a "Connected" property.
-- Therefore I am removing the check for such a property and instead checking
-- if the Timer still exists as you delete it a few lines later with "Timer = nil"
if Timer and typeof(Timer) == "RBXScriptConnection" then
Timer:Disconnect()
Timer = nil
task.wait()
print("disconnected")
gui.Text = "00:00.00"
end
this works most of the time, but i have no idea why its sometimes not working
btw, this is the serverside script of the resettimer
local winpad = script.Parent
winpad.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local humanoidrootpart = hit.Parent:FindFirstChild("HumanoidRootPart")
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if humanoid then
player.Team = game.Teams:FindFirstChild("Winners")
humanoidrootpart.CFrame = CFrame.new(139.742, -42.375, 108.665)
game.ReplicatedStorage.ResetTimer:FireClient(player)
end
end)
game.ReplicatedStorage.ResetTimer.OnClientEvent:Connect(function()
print(gui.Text)
if typeof(Timer) == "RBXScriptConnection" then
if Timer.Connected then
Timer:Disconnect()
Timer = nil
task.wait()
print("disconnected")
gui.Text = "00:00.00"
end
end
end) -- localscript
local winpad = script.Parent
winpad.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local humanoidrootpart = hit.Parent:FindFirstChild("HumanoidRootPart")
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if humanoid then
player.Team = game.Teams:FindFirstChild("Winners")
humanoidrootpart.CFrame = CFrame.new(139.742, -42.375, 108.665)
game.ReplicatedStorage.ResetTimer:FireClient(player)
end
end) -- serverscript