Collecting user input only once?

Currently making a customizable keybinds tab, I’m trying to only collect a player’s input only once with UserInputService.

Problem is, once I get that first value, I can’t find a way to break out of that InputBegan event.

My current system/attempt:
I have a button, once clicked, it has the InputBegan event within it. Once you press it, it starts printing input data to the console.

image

My outcome:
image

My desired outcome:
Once I input anything as a player, the event disconnects and stops printing.

My relevant code:

ToggleCameraModeButton.MouseButton1Click:Connect(function()
	KeybindsTitle.Text = 'Input a key to be binded. \n Press backspace to cancel.'
	
	UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
		if input.KeyCode ~= Enum.KeyCode.Backspace and input.UserInputType == Enum.UserInputType.Keyboard then
			print(input.KeyCode)
		elseif input.KeyCode == Enum.KeyCode.Backspace then
			KeybindsTitle.Text = 'Keybinds'
		
		end
		
	end)
	
end)

If I didn’t explain that well enough, let me know. Not sure if I explained this accurately.

2 Likes

Just store the connection, and then disconnect it after the input has been received:

local Connection

Connection = UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.KeyCode ~= Enum.KeyCode.Backspace and input.UserInputType == Enum.UserInputType.Keyboard then
		Connection:Disconnect() --disconnect the connection
        Connection = nil
	elseif input.KeyCode == Enum.KeyCode.Backspace then
		KeybindsTitle.Text = 'Keybinds'		
	end
end)

Every RBXScriptSignal returns a RBXScriptConnection

6 Likes

Thanks, this works perfectly. Much appreciated. :+1:

3 Likes