How to check for enum.UserInputType or enum.KeyCode with no errors?

local keybinds = KeybindManager.keybinds
local keybind = keybinds[tool.Name][script.Name]
	
local success, result = pcall(function()
	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.

The same thing would happen if I tried this and just made it a variable:

local enumType = Enum.UserInputType[keybind] or Enum.KeyCode[keybind]

When testing, I get errors like these:
R is not a valid member of "Enum.UserInputType"

2 Likes

You don’t need pcalls for this, but I don’t understand what you want to do exactly?

1 Like

Like as @SomeFedoraGuy, i don’t understand what you wanna do, but why you need UserInputType, if you only have keycode in variables?

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

game.UserInputService.InputBegan:Connect(function(Key)
	if Key.KeyCode == keybind then
		print("Worked")
	end
end)
1 Like

A keybind system that lets you change keybinds to mouse input or just a key on your keyboard, which is why I want to check BOTH userInputType and keyCode without any errors

A keybind system that lets you change keybinds to mouse input or just a key on your keyboard, which is why I want to check BOTH userInputType and keyCode without any errors

keycode:IsA(“UserInputType”), keycode:IsA(“KeyCode”) respectively

(just did something with this so im 90% sure but let me double check)

1 Like

I think there’s something like this called EnumType which returns something like that, not rlly sure though

There is, it points to the parent enum. Could also use this if you want. IsA is probably more future proof tho

1 Like

IsA works on Enums? I never knew that, how do you use it?

Nevermind, figured it out

local z = Enum.KeyCode.Z
print(z:IsA("KeyCode"))

prints true

Honestly, i’m still don’t understand you completely.

You wanna do system, that allows you to change key, binded to some function, and binded key can be mouse click or keyboard button press. Right?

1 Like
local binds = {
	{Enum.KeyCode.F,function()
		print("F")
	end,},
	{Enum.UserInputType.MouseButton1,function()
		print("MB1")
	end,},
}

game:GetService("UserInputService").InputBegan:Connect(function(inp,gp)
	if gp then return end
	
	for _,v in binds do
		if inp[tostring(v[1].EnumType)]==v[1] then
			v[2]()
			break
		end
	end
end)

condensed logic for ur goal. Could also use IsA with individual cases but by using enumType you can allow for more than 2 enum types in ur binds table, without having to alter the code in the loop, if roblox ever adds others.

1 Like

Lets say I have a gun, now, the user wants to change their gun shooting keybind from mouse click to key E, but the problem is that, the keys use Enum.KeyCode while your mouse uses Enum.UserInputType, so how can we make it work for both of these?

Okay ill take a look at this, thanks

Depends on how you detect the keybinds. If you use a classic click a box, then listen for keyboard/mouse input, you can use the same logic as above to determine if you have a UserInputType or a KeyCode. And then rewrite that bind accordingly.

Also, my problem is that im not directly storing the keybinds as enums, im storing them as a string so that I can save them to datastores, so this wouldn’t really work

local KeybindManager = {
	keybinds = {
		["Dribble"] = {
			["Dribble"] = "MouseButton1",
			["Right Sided Dribble"] = "R"
		}
	}
}
KeybindManager.defaultKeybinds = KeybindManager.keybinds

return KeybindManager

Its best practice to store the EnumType explicitly in another variable, or just something like “UserInputType/MouseButton1” to the datastore then when you need it in runtime you can just convert that string to an actual enum.

OR you could write a function to tell you if youre looking at a user input type or enum. Something like this:

local function getEnumType(enumStr)
	for _,v in Enum:GetEnums() do
		if v:FromName(enumStr) then
			return v
		end
	end
end

Note, this only works with certain enum types but you could make it work with all if u switch to recursion.

Id go with first option, as its more explicit to just store the enumtype in the string itself

1 Like

Here

local keybinds = KeybindManager.keybinds

local MouseBinds = {
	Enum.UserInputType.MouseButton1,
	Enum.UserInputType.MouseButton2,
	Enum.UserInputType.MouseButton3
}

game.UserInputService.InputBegan:Connect(function(Key)
    local keybind = keybinds[tool.Name][script.Name]
	if table.find(MouseBinds, Key.UserInputType) and Key.UserInputType == keybind then
		print(Key.UserInputType.Name.." Was clicked")
	elseif Key.KeyCode == keybind then
		print(Key.KeyCode.Name.." Was pressed")
	end
end)

Works very simple: if UserInputType is in table and it’s equal to our keybind, then print something,
Else our key - keyboard then we check if it equals to our keybind and print something

local KeybindManager = {
	keybinds = {
		["Dribble"] = {
			["Dribble"] = "Enum.UserInputType.MouseButton1",
			["Right Sided Dribble"] = "Enum.KeyCode.R"
		}
	}
}

so what if I stored it like this, then how would I detect it? I dont know how to convert that string to an enum

local Name = "R"

local Bind

for	i, Enums in Enum.KeyCode:GetEnumItems() do
	if Enums.Name == Name then
		print("Got")
		Bind = Enums
		break
	end
end

if not Bind then
	for	i, Enums in Enum.UserInputType:GetEnumItems() do
		if Enums.Name == Name then
			print("Got")
			Bind = Enums
			break
		end
	end
end

Just bruteforcing and set bind into variable