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 8 years ago
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

Log in to vote
0
Answered by
Kryddan 261 Moderation Voter
8 years ago
Edited 7 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.

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)
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 — 8y
0
My misstake let me fix it. Kryddan 261 — 8y
0
I see scottmike0 40 — 8y
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 — 8y
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 — 8y
0
indent code plox ScriptGuider 5640 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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.

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

Answer this question