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!
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)