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

How can I tell if a User is holding down a button?

function onKeyPress(inputObject,gameProcessed)
if inputObject.KeyCode == Enum.KeyCode.V then
--do stuff
end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)-- an event for buttons on keyboard

using this event, along with a key event, only sends it once, when the inputObject is pressed. I am aiming for the user to hold down a button, but there is not options for it at all. I have tried while loops, ends up crashing, I also have tried using isKeyDown, which only tells me if the key is part of the keyboard...

2 answers

✓ Accepted answer

One method could be to add a debounce and a while loop, for example when you press the button you change the debounce to true and when the button is released, make it false. Then all you have to do is use a while debounce == true do loop.

KeyHeld = false

function onKeyPress(inputObject,gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.V then
		KeyHeld = true
		while KeyHeld then
			--do stuff
			wait()
		end
	end
end

function onKeyRelease(inputObject,gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.V then
		KeyHeld = false
	end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)
game:GetService("UserInputService").InputEnded:connect(onKeyRelease)

6 comments

0

the concept sounds nice, but I just tried that code. And all I did was end up spamming bricks that were in the center. of the map. I tried pressing V to stop that did not work either.

0

My misstake let me fix it.

0

I see

0

Soo, What I did not know is that you can have more than two of the same types of Services, but in different actions. Though What I really did not know was the significance of Input Ended, since there was no example code on the Api

0

You can use a variable of userinputservice, local uis = game:GetService("UserInputService"). Then use it like this, uis.InputBegan, and for the InputEnded part there is a example code, http://wiki.roblox.com/index.php?title=API:Class/UserInputService/InputEnded

0

indent code plox

This is how I've been using controls for my games:

local tabDown = false
local input = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

input.InputBegan:connect(function(k)
	local key = k.KeyCode
	if key == Enum.KeyCode.Tab then
		tabDown = true
	end
end)

input.InputEnded:connect(function(k)
	local key = k.KeyCode
	if key == Enum.KeyCode.Tab then
		tabDown = false
	end
end)

while wait() do
	if tabDown then
		print("Tab is down")
	else
		print("Tab is not down")
	end
end

It works very well for me. Of course in your case you would have to change the Enum for the key but other than that it should work.

1 comment

0

can u pls make the script not firing it when typing?

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