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”
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.
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)
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)
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