How to detect whether if a player is holding or tapping a key

I’m using UserInputService:JumpRequest to detect when a player is jumping, I was wondering how I can implement if a player is holding space or tapped it in the function.

1 Like

Create a while loop inside your InputBegan function for the respective key, and make it check for a variable flag that the InputEnded function for the respective key sets to true.

What you wish to do when the user is holding down the key should happen inside the while loop. You’ll need to use wait() function inside the while loop so it doesn’t overrun the program. Wait(0.1) will run the while loop 10 times per second, until the key is released, for example.

And the first thing to do in InputBegan and InputEnded for the key is to set the flag variable to false or true. InputEnded will stop the while loop in the InputBegan function and the next time InputBegan for that key is called the flag for the variable should be set to true so that the loop can execute again.

you could use jumprequest, and then, wait however long you need to then use UIS:IsKeyDown()

so for example

blablahjumprequest:Connect(function()
task.wait(t)
if UIS:IsKeyDown(Enum.Keycode.Space) then
print("player held space")
else
print("player tapped space")
end)
1 Like

it somewhat works but i’m trying to make a wallrun system so the wait makes it look kinda clunky

maybe try wrapping it inside of RunService like so:

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

RunService.RenderStepped:Connect(function()
	if UserInputService:IsKeyDown(Enum.KeyCode.Space) then
		print("Space is being held")
	else
		print("Player tapped space")
	end
end)

if you could expand on what you’re using this mechanic for, how does the wallrun you want to create work? what do you want to accomplish with the look and feel of it?

It’s not clunky, you can set it to update faster than even the framerate of the game. I recommend you use a different key to engage wall running so that you don’t have to differentiate whether the player wants to wall run or not when they start a jump. This will remove a wait() grace period delay that you need to determine that the player is holding down the key purposefully vs just tapping the key. You can code the jump key to end a wall run if needed.