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

Detect if a key is being held then let go (keydown?)

I've tried it many ways but I couldn't figure out how. I'm trying to do this with UserInputService but i've had no luck. I can make it so that if a key is pressed, something functions but i can't seem to figure out how to detect if a key is not being held anymore

If anybody could help me out that would be great

What i wanted to do is for example this script:

local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local rotate = false


--while wait(0.1) do
--if rotate == true then
--print("Rotating....")
--else
--print("Not Rotating")
--end
--end


mouse.KeyDown:connect(function (key)
	for i, v in pairs(game.Players:GetChildren()) do
	if key == string.char(19) and v.Name == Player.Name then
	rotate = true
	
	end
	mouse.KeyUp:connect(function (key)
	if key == string.char(19) and v.Name == Player.Name then
rotate = false
	end 
end) 
end 
end)

You get the point, the above script is an example and is inefficient + outdated but you should get the point.

Thanks in advance

1 answer

#The problem Don't use keydown, use userinputservice! If you don't know how it works don't worry. They're VERY similar. The main problem was that you put KeyUp inside of the key up function.


#UserInputService This service gets any local input from the player whether it is a mouse clicking or someone pressing the left shift key specifically. You can use InputBegan and InputEnded instead of KeyDown and KeyUp. You use keycode to find the key.

local uis = game:GetService("UserInputService")

uis.InputBegan:connect(function(input)
	print("The user inputted a keycode of "..input.KeyCode)
end)

uis.InputEnded:connect(function(input)
	print("The user stopped their input of "..input.KeyCode)
end)

If they pressed the "E" key it'll print out

The user inputted of Enum.KeyCode.E

The user stopped their input of Enum.KeyCode.E

if they click then it'll say

The user inputted of Enum.KeyCode.Unknown


#The error You put the keyup function inside of the keydown! That means you need to put it outside of it and make it's own function because it'll only run if you take your finger off the key the second you press it. Also, when using infinite loops in a script make sure you always put it after all your functions or even better: the end of the script so that other functions can run properly. The code is read like a book top to bottom. If it loops infinitely it can't go to the next function.

local Player = game.Players.LocalPlayer --Make sure it's a local script!
local rotate = false
local uis = game:GetService("UserInputService")

function IsHeld() --Turned loop into a function
	local t1 = tick()
	repeat game:GetService("RunService").Heartbeat:wait() until not rotate --Keeps looping at ~1/60 of a second.
	print(string.char(19).." has been pressed for about "..tick()-t1.." seconds")
end --I used math.floor to round the value down to a whole number.

uis.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.Pause then
		rotate = false --changes rotate
	end
end)

uis.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.Pause then --In enums, 19 is pause. ROBLOX uses Dec codes for ascii. 19 is device control 3. Using Enum.KeyCode.[Key] should be used though to make it easier.
		rotate = true
		IsHeld()
	end
end)

Hope it helps!

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