Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago
1function onKeyPress(inputObject,gameProcessed)
2if inputObject.KeyCode == Enum.KeyCode.V then
3--do stuff
4end
5end
6game: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

Log in to vote
0
Answered by
Kryddan 261 Moderation Voter
9 years ago
Edited 8 years ago

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.

01KeyHeld = false
02 
03function onKeyPress(inputObject,gameProcessed)
04    if inputObject.KeyCode == Enum.KeyCode.V then
05        KeyHeld = true
06        while KeyHeld then
07            --do stuff
08            wait()
09        end
10    end
11end
12 
13function onKeyRelease(inputObject,gameProcessed)
14    if inputObject.KeyCode == Enum.KeyCode.V then
15        KeyHeld = false
16    end
17end
18 
19game:GetService("UserInputService").InputBegan:connect(onKeyPress)
20game:GetService("UserInputService").InputEnded:connect(onKeyRelease)
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. scottmike0 40 — 9y
0
My misstake let me fix it. Kryddan 261 — 9y
0
I see scottmike0 40 — 9y
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 scottmike0 40 — 9y
View all comments (2 more)
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 Kryddan 261 — 9y
0
indent code plox ScriptGuider 5640 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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

01local tabDown = false
02local input = game:GetService("UserInputService")
03local player = game.Players.LocalPlayer
04 
05input.InputBegan:connect(function(k)
06    local key = k.KeyCode
07    if key == Enum.KeyCode.Tab then
08        tabDown = true
09    end
10end)
11 
12input.InputEnded:connect(function(k)
13    local key = k.KeyCode
14    if key == Enum.KeyCode.Tab then
15        tabDown = false
View all 25 lines...

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.

0
can u pls make the script not firing it when typing? nobb93815_newaccount 0 — 6y

Answer this question