How to detect specific keys?

I have a running script, But im assuming that when InputEnded fires, it fires for when any input that is no longer entered. which is a problem. that means if a player is holding shift and they let go of a movement key, their run won’t apply.

How would i use UserInputService to detect when a player lets go of a Specific key?

1 Like

yes it fires for any input but it also returns the key that was let go for example you can do:

game:GetService("UserInputService").InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
         -- Do code
	end
	
end)
3 Likes

oh, i didn’t know it’d do that. i’ll test it right now

Yeah, just remember if you are not sure with something you can always just check the dev hub and the related code samples:

https://developer.roblox.com/en-us/api-reference/event/UserInputService/InputEnded

another quick question that just came up, i notice that when i press LeftShift, InputBegan is not firing, is this because ShiftLock is on? would i have to disable shiftlock in order to have this work or is there a way to bypass this and have both work

are you sure I just tested it and it worked for me this is what I used, it is basically just the same code:

game:GetService("UserInputService").InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		-- Code
	end
	
end)

Oh i forgot to change a value from it, yeah it works

is there a way to apply this to a loop though? like i have this loop that constantly updates the character’s speed but i don’t know how to impliment the specific key to this function so it stops looping when the player lets go of that key

loop:

repeat
					Char.Humanoid.WalkSpeed = Char.Humanoid.WalkSpeed + 1
					print(Char.Humanoid.WalkSpeed)
					wait(0.2)
				until Char.Humanoid.WalkSpeed == 50 or UIS.InputEnded

you could do something like this:

local running = true

game:GetService("UserInputService").InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
         running = false
	end
end)

repeat
	Char.Humanoid.WalkSpeed = Char.Humanoid.WalkSpeed + 1
	print(Char.Humanoid.WalkSpeed)
	wait(0.2)
until Char.Humanoid.WalkSpeed == 50 or not running
1 Like

Works perfectly, i may have to tweak a few things with debounce to prevent spam but it works just as i wanted, thank you so much for the help!

1 Like