How do I do this?

Hi guys! I’d like to know how I can disconnect a RBXScriptSignal. I want the code to stop at a certain point, but I’m not sure what to do to disconnect it. I’ve used variables to define the function and disconnect it that way, but that usually doesn’t work unless I’m just dumb or something. If I am being dumb right now, by all means, let me know lol.

CODE:

local function Countdown(TimeLeft)
	local ShouldRun = true
	RunService.Heartbeat:Connect(function(deltaTime)
		if ShouldRun == true then
			ShouldRun = false
			TimeLeft.Value -= 0.1
			task.wait(0.1)
			if TimeLeft.Value <= 0 then
				UnderAttack.Value = false
                ShouldRun = false
             -- What do I put here?
			end
		end
	end)
end

Using disconnect should work, when you did it did it look like this?

local function Countdown(TimeLeft)
	local ShouldRun = true
    local Signal
	Signal = RunService.Heartbeat:Connect(function(deltaTime)
		if ShouldRun == true then
			ShouldRun = false
			TimeLeft.Value -= 0.1
			task.wait(0.1)
			if TimeLeft.Value <= 0 then
				UnderAttack.Value = false
                ShouldRun = false
                Signal:Disconnect()
                return
			end
		end
	end)
end

Even if it did just give it a try and lmk

1 Like

Hey! This worked! There’s only a slight problem though. The value is changing by 0.2 instead of 0.1. Here is the code I have now. Thanks so much if you can help!

local function Countdown(TimeLeft: NumberValue)
	local ShouldRun = true
	local RBXSignal
	RBXSignal = RunService.Heartbeat:Connect(function(deltaTime)
		if ShouldRun == true then
			ShouldRun = false
			if TimeLeft.Value <= 0 then
				UnderAttack.Value = false
				print("Disconnecting...")
				RBXSignal:Disconnect()
				return
			else
				TimeLeft.Value -= 0.1
				task.wait(0.1)
				ShouldRun = true
				print("Running. Time left: "..TimeLeft.Value)
			end
		end
	end)
end

I believe the value is ‘changing’ by 0.2 because your print statement is at the end of the function. Moving it to the top should print the correct value.

1 Like

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