I'm making a keybind in my game and I have a value to save the key that needs to be pressed. I can't figure out which value to use to store the value. string won't work. Thanks in advance.
✓ ANSWER ACCEPTED
Which Value to use for UserInputService?
1 answer
✓ Accepted answer
It’s actually best you store the KeyStroke as a variable in the LocalScript, it’ll save space and is a lot less of a hassle to prepare. Since the InputObject is usually compared to the KeyCode Enum, it’s best to store the desired Key accordingly. Using this Enum as well, is a lot more versatile too.
local UserInputService = game:GetService("UserInputService")
local KeyStroke = Enum.KeyCode.A --// KeyStroke
UserInputService.InputBegan:Connect(function(InputObject, GameProcessed)
if ( InputObject.KeyCode == KeyStroke ) then
print(game.Players.LocalPlayer.." Pressed the set keyStroke!")
end
end)
If you deeply desire to use a Value Object to reference you KeyStroke, then you'd be using a StringValue, located in ReplicatedStorage so both Script types can acces it. The Value must be a capitalized letter, as the InputObject compares to a capitalized String.
local UserInputService = game:GetService("UserInputService")
local KeyStroke = game:GetService("ReplicatedStorage").KeyStroke --// 'A'
UserInputService.InputBegan:Connect(function(InputObject, GameProcessed)
if ( InputObject.KeyCode == KeyStroke ) then
print(game.Players.LocalPlayer.." Pressed the set keyStroke!")
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

5 comments
Ok but can I access the variable from outside the script and if so how. I need to access to the variable from everywhere
You cannot, that wouldn't work. It's generally the same thing though as an Instance to index, just rewrite the variable.
I've updated my answer
I still suggest using the first method with the KeyCode Enum as I said, it is a lot more versatile, and ultimately easier to use.
thx