Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

6 comments

0

Thank you I will try it

0

Where do I put it

0

In any localscript that is in startergui

0

what if its not in starter gui?Can i put it in  a model in workspace It works in startergui though 

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

0

I need to check if it works

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

0

Where do I put it!

0

UserInputService is a local service, so it should be put in a local script

0

anywhere in a localscript?

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!

0

wtf how is this accepted with no explanation.

This discussion is preserved as it was. New answers, comments, and votes are closed.