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"
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)
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
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.
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?
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
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
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 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