Keybind system doesn't work for handling KeyCode and UserInputType

local keybinds = KeybindManager.keybinds
local keybind = keybinds[tool.Name][script.Name]
	
local success, result = pcall(function(...): R...
	return Enum.UserInputType[keybind] or Enum.KeyCode[keybind]
end)
if not success or not result then
	return
end
if input.UserInputType == result or input.KeyCode == result then
	print("Worked")
end

I am trying to make a keybind system that will handle both KeyCode and UserInputType, I don’t want to do anything too complicated, but my current system isn’t working.

Basically, it isn’t working because it will evaluate the keybind that I put first in the return statement, let’s say if the first value in the return was MouseButton1, right now it would work, but if I switched it to a key like E then it wouldn’t work because it would detect that there isn’t a enum.UserInputType called “E”, which means that it would just exit out of the whole function, preventing it from ALSO checking if there is a Enum.KeyCode for the keybind.

(Also, just ignore the weird typechecking thing on the pcall, im using anonymous autofill)

local keybinds = KeybindManager.keybinds
local keybind = keybinds[tool.Name][script.Name]

local result = Enum.UserInputType[keybind] or Enum.KeyCode[keybind]
if input.UserInputType == result or input.KeyCode == result then
	print("Worked")
end

No reason to use a pcall function here.

1 Like

Wow, for some reason, I did that exact same thing in a older version, but it errored. I just tried using a pcall() to fix it, but I guess I just did something wrong, I will continue to test this and see if it works, thanks!

1 Like
if input.UserInputType == Enum.UserInputType[keybind] or input.KeyCode == Enum.KeyCode[keybind] then
	print("Worked")
end

Okay, Your version works, it was because in my old version I just did it like this, I don’t really understand how making it a variable works differently, but I guess it’s something to do with how the comparison works, thank you

Doesn’t work, same exact issue.

Ah, I see! It looks like the issue is that the way Enum.UserInputType and Enum.KeyCode are being handled doesn’t account for keybinds that might be a keyboard key (KeyCode) or a mouse button (UserInputType). The issue arises when you try to compare both at once and it doesn’t check properly if the input matches either enum type.

Here’s a more detailed breakdown and a better fix for your issue:

  1. Direct comparison will fail if the keybind is not part of the correct enum, which causes the issue you’re seeing. For instance, if keybind is "E", it only matches Enum.KeyCode and doesn’t match Enum.UserInputType, but in your code, both comparisons happen in the same if statement, which causes an early exit when one part fails.
  2. Handling KeyCode and UserInputType separately: We need to ensure that if keybind is intended to be a key, we check it with Enum.KeyCode; if it’s a mouse button or touch, we check it with Enum.UserInputType. By handling them in a separate, specific check, we can avoid those issues.

Correct Solution

We need to identify whether the keybind refers to a key or a mouse button and then compare against the appropriate enum.

Copiar código

local keybinds = KeybindManager.keybinds
local keybind = keybinds[tool.Name][script.Name]

local success, result = pcall(function()
    -- Try to get UserInputType (e.g., MouseButton1, Touch, etc.)
    local userInputType = Enum.UserInputType[keybind]
    if userInputType then
        return userInputType
    end
    
    -- If that fails, try to get KeyCode (e.g., E, W, etc.)
    local keyCode = Enum.KeyCode[keybind]
    if keyCode then
        return keyCode
    end

    -- Return nil if neither is valid
    return nil
end)

if success and result then
    -- Handle both UserInputType and KeyCode separately
    if input.UserInputType == result or input.KeyCode == result then
        print("Worked")
    end
end

Why this works:

  1. Enum handling in isolation: Instead of doing both checks in one statement, we check separately for Enum.UserInputType[keybind] (mouse/touch) and Enum.KeyCode[keybind] (keyboard key). This way, we don’t run into issues where one enum type is nil (which would stop the comparison).
  2. Separation of concerns: We try each enum type independently. If keybind is "E", it will only be matched with Enum.KeyCode, and if it’s "MouseButton1", it will be matched with Enum.UserInputType. This prevents comparing a KeyCode with a UserInputType (which would be invalid).
  3. Safe fallback with pcall: We’re using pcall to handle any invalid or missing enums gracefully, so it doesn’t throw an error if keybind isn’t found in either enum.

Troubleshooting:

If you’re still encountering issues, here’s a checklist to ensure everything works as expected:

  • Check keybind contents: Print the keybind to verify that it corresponds to a valid key or mouse button.

Copiar código

print(keybind)
  • Check what input is: Ensure that the input object (which could be input.UserInputType or input.KeyCode) is being properly passed and is in the correct format.

Copiar código

print(input.UserInputType)
print(input.KeyCode)

Final Thoughts:

This solution ensures that if keybind corresponds to a mouse button, we check it against Enum.UserInputType, and if it corresponds to a keyboard key, we check it against Enum.KeyCode. Let me know if this resolves the issue!