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

How to check if a player held down the key or just pressed it? [closed]

Asked by
Grazer022 128
3 years ago

so I’m doing a script, but i don’t know how to check if the player is holding or just pressed the key. I wanted to do a test script that checks it... but i don’t know how.

print(“player is holding down key”)
print(“player pressed key”)

so what i wanna do is that when a certain key is pressed (let’s do E for example) it will print “player pressed key”, else if E is held down, it will print “player is holding down E”). Please help me. THANKS!

0
Userinputservice JesseSong 3916 — 3y

Closed as Not Constructive by WideSteal321 and JesseSong

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
1
Answered by 3 years ago

So im not sure if you know how to use Inputs or not but i will show you a script and try to explain to my fullest on how it works:

local holding = false

game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    if input.KeyCode == Enum.KeyCode.E and db then

so in this first 5 lines of code what is happening is we have a variable which tells weather or not the key is being held or not which ill use in a bit.. so the script is detecting any inputs on line 1 (from the keyboard) and after it detects them, on line 2 it is checking if the input was the player typing or chat, if it was then the script ends but if it wasnt it continues. on line 3 it now checks what the input was, which for you it will be E. after the key E is detected being clicked:

game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    if input.KeyCode == Enum.KeyCode.E  then
        holding = true

        while holding == true do
            print("Holding")
            wait(.5)
        end
    end
end)

so after the first 5 lines i have set the variable "holding" to true because now the player is holding the the key E. after it sets the variable value we have a loop which is now checking that while the variable is equal to true to print "Holding" in the output to show you as a test the character is holding the key every 0.5 seconds.(dont get rid of the wait(0.5) or it will cause extreme lag in the loop and cause your studio to crash or the script wont work in general)

so now your probably thinking.. well how do you know if they arent holding the key anymore?

game:GetService("UserInputService").InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        holding = false
    end
end)

next a few lines down outside of the first function we add these lines of code.. this piece of code similar to the last one detects any inputs and if its the key E or not except its detecting any input thats Ending, now that it detected that the input E ended it will set the variable value of holding to false causing the first part of the script to stop print "Holding" meaning that the player isnt holding the key E anymore. well i hope this made sense and helped you :)

here is the whole script:

local holding = false

game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    if input.KeyCode == Enum.KeyCode.E and db then
        holding = true

        while holding == true do
            print("Holding")
            wait(.5)
        end
    end
end)

game:GetService("UserInputService").InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        holding = false
    end
end)
0
He was asking for how to detect if the player tapped E or held it. Not how to detect if the player was chatting or not. xxIamInevitable 2 — 3y
Ad