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

How to check if a key is being held down?

I’m using UserInputService, I know how to check if a key was pressed, but I’m not sure how to tell if it’s being held down, how would I do that? How would I check if It weren’t held anymore too?

3 comments

0

InputBegan for held down, InputEnded for released

0

I remember a more efficient way of doing it, are you sure that’s the only method?

0

I’m trying to create an event when the ‘E’ key is being held, whilst Left-Clicking, this is to drop an Item, I assumed it wans’t exactly possible to tie both events together, so I’m using a Boolean variable as a tie instead, just using it in am if statement using the Button2Down event, I don’t want to go through all this trouble to use up so much space for a simple Boolean switch

2 answers

✓ Accepted answer

You can use the :IsKeyDown() method of UserInputService. Just pass the key you want and :IsKeyDown() will return true if the key is being held down, false otherwise.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gpe)
	if gpe then return end

	if input.UserInputType == Enum.UserInputType.MouseButton2 then -- you can detect mouse input
		if UserInputService:IsKeyDown(Enum.KeyCode.E) then
			-- Code
		end
	end
end)

######Hopefully this answered your question, and if it did, then don't forget to hit that "Accept Answer" button, and optionally, give an upvote. It helps out a lot. If you have any other questions, then feel free to leave them down in the comments.

2 comments

0

I love your style of answering, it just has a tad bit of "unique" to it.

0

Thank you!

First, insert a LocalScript into the StarterPlayerScripts in the StarterPlayer. Remove the print function and replace it with this:

local UserInput = game:GetService("UserInputService") -- So we can fire the function below

local function inputR(inputObject, gameProcessedEvent)
	
end

UserInput.InputBegan:Connect(inputR)

Now insert a simple if statement inside the local function with a return.

local UserInput = game:GetService("UserInputService") -- So we can fire the function below

local function inputR(inputObject, gameProcessedEvent)
	if gameProcessedEvent then return end -- I did it this way so it doesn't take up space.
end

UserInput.InputBegan:Connect(inputR)

Now make sure the user has a keyboard, so no weird errors occur.

local UserInput = game:GetService("UserInputService") -- So we can fire the function below

local function inputR(inputObject, gameProcessedEvent)
	if gameProcessedEvent then return end
	if inputObject.UserInputType == Enum.UserInputType.Keyboard then
		
	end
end

UserInput.InputBegan:Connect(inputR)

Now test the key you would like to be pressed. I Added more comments to make more clear.

local UserInput = game:GetService("UserInputService") -- So we can fire the function below

local function inputR(inputObject, gameProcessedEvent)
	if gameProcessedEvent then return end
	if inputObject.UserInputType == Enum.UserInputType.Keyboard then -- So we know that the player is holding down a key on their keyboard instead of a fake one
		if inputObject.KeyCode == Enum.KeyCode.R then -- Replace "KeyCode.R" with any other key, for example, left shift is "Enum.KeyCode.LeftShift". --
			--Do code for your key pressed here
		end
	end
end

UserInput.InputBegan:Connect(inputR) -- So we can fire the function when any key/mouse is pressed. Anything else will also fire the function, like gamepad.

I hope this was helpful!

1 comment

0

Uh, I am a master Lua scripter now man. This is easy, you also got it wrong, is just as easy as one line... :IsKeyDown(), a method of the UIS family. This post is also a year old..

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