Making customizable keybindings

  1. What do you want to achieve? I’m making a poses\animations GUI. When the player presses one of the buttons, the corresponding animation plays. So far, so good… I also wanted to add customizable keybindings where the player can input whatever letter or number they want in the matching textbox and by pressing that same key, the animation will play.

  2. What is the issue? When trying to index the textbox’s Text with the UserInputType (input.UserInputType == Enum.KeyCode.kb) the console gives me this error:
    image

Here is the full code in a LocalScript:

    local id = script.Parent.Animation
    local plr = game.Players.LocalPlayer
    local kb = script.Parent.keybind
    local UIS = game:GetService("UserInputService")

    --Play animation function
    local function playAnim()
        local char = plr.Character
        local humanoid = char:FindFirstChildOfClass("Humanoid")
    if humanoid then
            local animator = humanoid.Animator
            if animator then
                local animationTrack = animator:LoadAnimation(id)
                animationTrack:Play()
                while animationTrack.IsPlaying do
                    wait(0.1)
                    char.HumanoidRootPart.Anchored = true
                end
                char.HumanoidRootPart.Anchored = false            
                return animationTrack
            end
        end
    end

    --Character cap in keybind TextBox
    kb.Changed:connect(function()
        kb.Text = kb.Text:sub(1,1)
    end)

    --Custom keybind (that's where the error is)
    local function keybind(input)
        if input.UserInputType == Enum.KeyCode.kb.Text then
            playAnim()
        end
    end

    --Triggers
    UIS.InputBegan:Connect(keybind)
    script.Parent.Activated:Connect(playAnim)

And here is the hierarchy, all in StarterGui of course:
image

  1. What solutions have you tried so far?
    After some attempts, I realized it’s impossible to index something to Enum.Keycode unless it’s the code of a key. The issue basically is to convert the .Text of the textbox into the code and then indexing it to Enum.Keycode.
    I tried searching in both the Wiki and the Forum, nothing seemed to help.
1 Like

What type of keybind is it to toggle the animation

What do you mean exactly?
If I understand your question correctly, the keybind can be decided by the user by inputting the letter or number they want in the TextBox. ^^

your problem is it is searching for a KeyCode called “kb” Instead, use this:

if input.UserInputType == Enum.KeyCode[kb.Text] then
end

Edit: I would also recommend using a check to see if it is a valid KeyCode like this:

if Enum.KeyCode[kb.Text] then
end
1 Like

If your trying to get the enum code for a key the keycodes are numbered by ascii value so you can get the ascii code with

Enum.KeyCode.B.Value == tonumber(string.byte("b",1)) -- equates to true

image
Tried both your methods, now it’s giving me this error… :weary:

That’s cool to know, but I don’t understand how that could help… ^^"
assuming I manage to find a way to implement your method, I’d have to do something similar for every letter and number, which is quite tedious.

you have to use :upper on the text like this (because KeyCodes are uppercase and not lowercase):

if Enum.KeyCode[kb.Text:upper()] and input.UserInputType == Enum.KeyCode[kb.Text:upper()] then
end

Or, you could simply on the line where you changed the Text with kb.Text = kb.Text:sub(1,1) use this:

kb.Text = kb.Text:sub(1,1):upper()
1 Like

image
Am I missing something? It seems like the script isn’t “logging” the Textbox’s Text now.

--Custom keybind
local function keybind(input)
    if Enum.KeyCode[kb.Text:upper()] and input.UserInputType == Enum.KeyCode[kb.Text:upper()] then
        playAnim()
    end
end

Oh, that makes the code a little bit neater, thank you. :slight_smile:

I just noticed this, but change the input.UserInputType to input.KeyCode sometimes it messes the code up. But here is the fixed script in whole if needed:

local plr = game.Players.LocalPlayer
local kb = script.Parent.keybind
local UIS = game:GetService("UserInputService")

local function playAnim()
	local char = plr.Character
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	if humanoid then
		local animator = humanoid.Animator
		if animator then
			local animationTrack = animator:LoadAnimation(id)
			animationTrack:Play()
			while animationTrack.IsPlaying do
				wait(0.1)
				char.HumanoidRootPart.Anchored = true
			end
			char.HumanoidRootPart.Anchored = false            
			return animationTrack
		end
	end
end

kb.Changed:connect(function()
	kb.Text = kb.Text:sub(1,1):upper()
end)

--Custom keybind (that's where the error is)
local function keybind(input)
	if string.len(kb.Text) > 0 then
		if Enum.KeyCode[kb.Text] and input.KeyCode == Enum.KeyCode[kb.Text] then
			playAnim()
		end
	end
end

--Triggers
UIS.InputBegan:Connect(keybind)
script.Parent.Activated:Connect(playAnim)
1 Like

Thank you so much! That works wonderfully. I was also trying to prevent errors if the Text was nil but you already did it, thank you once again! One last question, is there a way to prevent the keybind from being triggered while typing in chat? Apologies if I ask so many questions.

add another parameter to the keybind function. After that, do a if statement to see if it is false.

1 Like