Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Making a control customizer: getting player key/controller input?

I'm currently working on a project and have decided to make a control customizer for it.

Example: You click on a GUI button labeled "use key" then press the button/key you want to assign to that function. That key is then set in a value "Use_key", so that I can do somthing like:

local Use_key =workspace.Use_key.Value
local Input_Service =game:GetService("UserInputService")

Input_Service.InputBegan:connect(function(input)
	if input.KeyCode ==Use_key then
		print("Use!")
	end
end)

So my question is this: How can I successfully detect the key (preferably in Enum.KeyCode format) that a player presses (on a keyboard OR Xbox controller), then assign that to a string value?

1 answer

✓ Accepted answer

It is pretty simple, you can convert your KeyCode to a string and save it inside a value.

You simply use the tostring() method that Lua has to convert the input to an actual string which you can input into a string value or a table.


game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
    if(input.KeyCode == Enum.KeyCode.E) then
        game.Workspace.ExampleStringValue.Value = tostring(input.KeyCode);
    end;
end);


This is the simple example to capture the input that you desire and save it as a string to a value.

You can check for the UserInputType in UserInputService to make sure to capture specific inputs such as the keyboard or mouse for example.

Here's a list with all the possible User Input types that you can check for: https://developer.roblox.com/en-us/api-reference/enum/UserInputType

An example on how to check first for User Input Type and then check for the KeyCode:


game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
    if (input.UserInputType == Enum.UserInputType.Keyboard) then
        if(input.KeyCode == Enum.KeyCode.E) then -- Notice that I check for the user input type here and only capture keyboard input.
            game.Workspace.ExampleStringValue.Value = tostring(input.KeyCode);
        end;
    end;
    if(input.KeyCode == Enum.KeyCode.E) then
        game.Workspace.ExampleStringValue.Value = tostring(input.KeyCode);
    end;
end);

This is all you need to know as for you to resolve your issue, although I would recommend you to use ContextActionService which is another way of capturing input of the player, in my opinion it is a better way than catching it through UserInputService because you can modularize your input from the beginning for various types of controls such as an XBoX Controller or a Touchpad.

Here is a link to the API explaining in detail about it:

https://developer.roblox.com/en-us/api-reference/class/ContextActionService

If you have any more questions make sure to ask them. Good luck!

This discussion is preserved as it was. New answers, comments, and votes are closed.