Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i see if the player is holding down a certain key?

Asked by 5 years ago
Edited 5 years ago

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!

2 answers

Log in to vote
1
Answered by
blockmask 374 Moderation Voter
5 years ago

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

0
Thank you I will try it turquoise_monkeyman 32 — 5y
0
Where do I put it turquoise_monkeyman 32 — 5y
0
In any localscript that is in startergui blockmask 374 — 5y
0
what if its not in starter gui?Can i put it in  a model in workspace It works in startergui though  turquoise_monkeyman 32 — 5y
View all comments (2 more)
0
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 blockmask 374 — 5y
0
I need to check if it works turquoise_monkeyman 32 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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)
0
Where do I put it! turquoise_monkeyman 32 — 5y
0
UserInputService is a local service, so it should be put in a local script EliteJcoombs 77 — 5y
0
anywhere in a localscript? turquoise_monkeyman 32 — 5y
0
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! EliteJcoombs 77 — 5y
0
wtf how is this accepted with no explanation. User#24403 69 — 5y

Answer this question