How would I go about making keybinds customizable?

Greetings! What I’m trying to do is that… Some games let you bind an ability to a certain key. How would I do that with UserInputService? I don’t need the whole script, I just need to know how to check if the inputObject’s KeyCode is equal to the string.

eg. if inputObject.KeyCode == Enum.KeyCode.AvailableKeysFolder.QKey.Value then ...

The “QKey” would be a StringValue called “QKey” and then its value would be the letter “Q”

I’m willing to give more info of what I mean.

Couldn’t you do tostring(inputObject.KeyCode) == tostring(Enum.KeyCode.AvailableKeysFolder.QKey.Value)

I’m actually working on one. I just save their keybinds into a folder and compare it within the script

1 Like

I tried that.

It says in the output key isn't a valid EnumObject

local Keybinds = Rep.Keybinds

if tostring(inputObject.KeyCode) == Keybinds.KeyBindName.Value then
    -- code
end

Try that

1 Like

Oop, that way didn’t work either. dang

What this is doing is looking for the KeyCode AvailableKeysFolder (which doesn’t exist) and checking its QKey property, then checking the Value of its QKey property. You probably want to do something like Enum.KeyCode[AvailableKeysFolder.QKey.Value], which gets the KeyCode that has the same value as AvailableKeysFolder.QKey.Value.

6 Likes

Yes, that’s what I mean! :face_with_monocle::grinning:

What I would do is store all the default key binds in a table and then give the player the option to customize them using UserInputService and then just compare the key codes to see if they are equal to eachother. If so, then fire the corresponding function.

local keyBinds = {
    shoot = Enum.UserInputType.MouseButton1,
    aim = Enum.UserInputType.MouseButton2
}

local function inputBegan(input, textBoxFocused)
    if keyBinds.shoot == input.UserInputType then
        --shoot
    elseif keyBinds.aim == input.UserInputType then
        --aim
    end
end

userInputService.InputBegan:Connect(inputBegan)
5 Likes

I know mouse.KeyDown is depreciated, but it’s perfectly functional and is slightly easier in this case, as it only needs a string, no Enums.

mouse.KeyDown:connect(function(key) -- key will be a string
if key == AvailableKeysFolder.QKey.Value then -- the Qkey must be a string value
-- do stuff
end
end)
1 Like

Correct me if I am wrong, but couldn’t you use Object Values?

For example, you push a key setting the keybind, the store the InputObject in an object value, then when referencing it you do ObjectValue.Value.KeyCode

just do this

if key.KeyCode  == Enum.KeyCode[AvailableKeysFolder.QKey.Value] then 
print("i know im a bit late to this")