I am making a game where you need to press a key to do something but I don't know how to! I tried but it didn't work I want to see if the player presses a certain key on their keyboard like "w" Please help!
How do i see if the player is holding down a certain key?
2 answers
There are several ways to do this. One can use UserInputService in order to accomplish this, another way is ContextActionService, and mouse.KeyDown, but I'm only going to tell you UserInputService and ContextActionService
When using UserInputService, I'd reccomend using a variable to define it, and then connecting it to an InputBegan function
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input, typing)
if input.KeyCode == Enum.KeyCode.W and not typing then
--What happens when they're holding W
end
end)
and then you have to close it off with the InputEnded
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input, typing)
if input.KeyCode == Enum.KeyCode.W and not typing then
--What happens when they're holding W
end
end)
UIS.InputEnded:Connect(function(input, typing)
if input.KeyCode == Enum.KeyCode.W and not typing then
--What happens when they finished holding W
end
end)
Now for ContextActionSerivce
store it in a variable, then bind the action
local Context = game:GetService("ContextActionService")
local functionR = function(actionName, actionInputState, actionInputObject)
if actionInputState == Enum.UserInputState.Begin then
--What happens when holding w
end
end
Context:BindAction(
"actionName",
functionR,
false, -- Makes a button on their screen(mobile)
"w" -- The button they need to press on Pc
)
I hope I helped, and don't forgot to accept my answer if this helped
This may work for your scenario:
game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.W then --Change W to the key you want
print("Player pressed the key W")
end
end)
5 comments
Where do I put it!
UserInputService is a local service, so it should be put in a local script
anywhere in a localscript?
late response, but userinputservice obviously doesn't have a player input. it autodetects the player since it's a local script and the script is obviously run locally on the player so no need to input the player. it should be put anywhere where it will autorun on the player, so startergui or something along those lines will work good. hopefully this gives some clarity to your question good luck!
wtf how is this accepted with no explanation.
This discussion is preserved as it was. New answers, comments, and votes are closed.

6 comments
Thank you I will try it
Where do I put it
In any localscript that is in startergui
what if its not in starter gui?Can i put it in a model in workspace It works in startergui though
Localscripts don't run in workspace,serversciptservice,and serverstorage. Also, please start getting in the habit of accepting answers when you found a fix by someone
I need to check if it works