Reconnect a connection after it's been disconnected

Basically, I have this:

local connection = nil

connection = UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent == true then
		return
	end
	if input.KeyCode then
		button.Text = input.KeyCode.Name
		ToolBarKeybindsFolder[button.Name].Value = input.KeyCode.Name
		connection:Disconnect()
	end
end)

How could I re-connect it later in the script? Thanks!

5 Likes

This is very silly but should work:
Note: this does not work due to some subtleties in closure rules, sorry.

--Create a function which toggles the event being 
--connected or disconnect whenever called
function Toggler(event, handler)
	local discon = nil

	local con = function()
		local connection = event:Connect(handler)

		return function()
			return discon(connection)
		end
	end

	local discon = function(connection)
		connection:Disconnect()

		return con
	end 

	return con
end

--Wrap event connection action
local disconnected = Toggler(UserInputService.InputBegan, Handler)

--Connect for the first time
local connected = disconnected()

--Disconnect
disconnected = connected()

--Reconnect
connected = disconnected()
1 Like

Thank you for testing it out before I even had a chance to! :joy:
You’re fast!

Also, where does UserInputService.InputBegan come into play here? I don’t see it mentioned.
Thanks!

1 Like

This version has an actual chance of working and doesn’t involve an ∞-order function

--Starts disconnected, call 
function Toggler(event, handler)
	local event = nil

	local function Toggle()
		if event == nil then
			event = event:Connect(handler)
		else
			event:Disconnect()
			event = nil
		end

		return event
	end

	return Toggle
end

--Create wrapper
--Event starts Disconnected
local toggle = Toggler(
	UserInputService.InputBegan,
	function (input, gameProcessedEvent)
		if gameProcessedEvent == true then
			return
		end

		if input.KeyCode then
			button.Text = input.KeyCode.Name
			ToolBarKeybindsFolder[button.Name].Value = input.KeyCode.Name
			connection:Disconnect()
		end
	end)

--Connect
toggle()

--Disconnect
toggle()

--Reconnect
toggle()

I think I understand your last question. In both examples, UserInputService.InputBegan is bound to event so that the statement event:Connect(handler) has the same effect as:

UserInputService.InputBegan:Connect(handler)
1 Like

ah, I see now, you have that comma after UserInputService.InputBegan :D

1 Like


it’s giving me an error… do I add a local?

thanks :D

Where is button defined? I don’t see it in your original code.

oh sorry, I meant line 250

250 is not a problem, it just wants you to put local before function.

Sorry for the delay:


image

My bad there are two variables with the same name:

function Toggler(event, handler)
	local conn = nil

	local function Toggle()
		if conn == nil then
			conn = event:Connect(handler)
		else
			conn:Disconnect()
			conn = nil
		end

		return conn
	end

	return Toggle
end
1 Like

THANK YOU SO MUCH :D

Put the inner function as local :pleading_face: (and even the topscope one :sob:)

1 Like

Making the inner local has no effect.

I think it does, it makes it so the autocomplete doesn’t see the function

It will do that, but what the autocomplete thinks doesn’t matter!

:joy:

Well it defines the function in general scope, making it accessible through fenv, on top of being bad practice