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!
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()
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:
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