:Disconnect() Not working?

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

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

You are not setting your variable Timer to what the function starttimer returns.
Try this instead:

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

still doesnt work. is there any other way to reset a timer?

Can you explain me with more details what happens when you reset the timer?

2 Likes

robloxapp-20230814-1011565.wmv (5.0 MB)

1 Like

i want to reset the timer when the player gets teleported to the winroom, but its random for some reason

1 Like

Does print(gui.Text) execute? Do you see an output in your console? (Also, if it does, does it always print?)

1 Like

Screenshot 2023-08-14 101526
yes. but when i try to print timer after timer:Disconnect(), it gives me connected for some reason (not the ‘disconnected’ in this image)

1 Like

so im trying to figure out how to stop the timer with other methods

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)

i think it has something to do with the way a character touches a winpad

Thats really weird, wasn’t it working before already?

Could it be the return Timer line?

ill try
vid btw
robloxapp-20230814-1046093.wmv (6.2 MB)

So when the player pretty much teleports to the room with the green part in it, that is when you want the timer to stop?

yes thats when i want the timer to stop

What is the code for the part that is trying to stop the timer, also where is it located and what type of script is it?

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

They do

im trying to stop the timer when ‘winpad’ part is touched