How to Disconnect the Function

Edit: I found the Solution but how can i Disconnect the Function a()?

MouseButton1Click will run one Time when i click it but just when “elseif bFrame.Visible == true then…” is not fired. But after a() is fired another Time MouseButton1Click will run two Times and it will be more if i repead it.

local lockBackButton = false
local backButton = ""
local aFrame = ""
local bFrame = ""
local cFrame = ""
local buttonA = ""
local buttonB = ""

function a()
	
	aFrame.Visible = true
	backButton.Visible = true
	
	backButton.MouseButton1Click:Connect(function()

	if lockBackButton == false then

		lockBackButton = true

			if aFrame.Visible == true then

				aFrame.Visible = false
				bFrame.Visible = true
				
			lockBackButton = false

			elseif bFrame.Visible == true then

				backButton.Visible = false

				cFrame.Visible = false
				aFrame.Visible = false
				
				task.wait(1)
				
				--More other Code
				
				lockBackButton = false
				
			end
			end
	end)
	end
				
a()

buttonA.MouseButton1Click:Connect(function()
	
	aFrame.Visible = false
	bFrame.Visible = true
	
end)

buttonB.MouseButton1Click:Connect(function()

	bFrame.Visible = false
	cFrame.Visible = false

end)
a:Disconnect()

This should disconnect the function “a”
Or inside the function where you want to stop the execution just write:

return

These both dont work


Oh okay sorry. Try this:

local connection
connection = function a()
-- blah blah the code inside function a()
--when you want to disconnect do this:
connection:Disconnect()
end

Basically, you set your function equal to a variable so its connection can later be disconnected to prevent lag.
Let me know if it works.

This also dont works it makes a red line under the a and says Expected ‘(’ when parsing function, got ‘a’

You cant disconnect a function, only event

EDIT: Dont missudnerstand thee function meanings, they are used to shorten your code, make it clear an easy to use.

local function a()
-- do stuff
end

local event = button.MouseButton1Click:Connect(a)
-- some stuff
event:Disconnect()
local b

 local function a()
	
	script.Parent.MouseButton1Click:Connect(function()
		print("1")
		b:Disconnect()
	end)
	
	
end


wait(5)

b = script.Parent:WaitForChild("TextButton").MouseButton1Click:Connect(a)

wait(5)

b = script.Parent:WaitForChild("TextButton").MouseButton1Click:Connect(a)

I did this, that also dont works, there are two “b = script.Parent:WaitForChild(“TextButton”).MouseButton1Click:Connect(a)” so i can see how the MouseButton1Click is firing twice

you can do this to disconnect the mousebutton1click event:

local function a()
	local Connection : RBXScriptConnection? = nil -- you can ignore "RBXScriptConnection", i just wanted to add type checking lol
	Connection = script.Parent.MouseButton1Click:Connect(function()
		print("Clicked")
		Connection:Disconnect()
	end)
end

a()

It’s important that you define the variable “Connection” before actually setting it, else you won’t be able to disconnect it.

1 Like

Thanks and so do i need this?: local Connection : RBXScriptConnection? = nil
Because u said i can ignore it

you can also just do: local Connection = nil. OR: local Connection

Thanks it worked


1 Like