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

How to detect if a player is holding down a button (eg.: R) on keyboard?

Asked by 4 years ago

I want to make a script which Enables a Part when R is held down and disables when the R isn't held down. I didn't find any working Script (LocalScript) on the internet. It would be the best if its a (server) Script.

0
Use userinputservice. Thepoint13 99 — 4y

1 answer

Log in to vote
0
Answered by
Prestory 1395 Moderation Voter
4 years ago
Edited 4 years ago

You can create a variable and use UserInputService to detect key holding like i have done below. Insert a local script into StarterGui and place the code i have provided for you below into it.

local UserInputService = game:GetService("UserInputService")
local KeyDown = false

UserInputService.InputBegan:Connect(function(inputObject) 
    if(inputObject.KeyCode==Enum.KeyCode.R)then
        KeyDown = true
        while KeyDown do
        print("R is being held")
        wait(0.2)
        end
    end
end)

UserInputService.InputEnded:Connect(function(inputObject)
    if(inputObject.KeyCode==Enum.KeyCode.R)then
     KeyDown = false
    end
end)

0
this isnt a request site...oh well, i wonder how long its gonna take before you're banned Thepoint13 99 — 4y
0
Thank you! However, I have a few questions if you don't mind (im a scripting noob): This script only works in StarterGUi or I can put it in Workspace or StarterPlayerScripts? There is any way to convert it to a Server Script? Hyperpublic 4 — 4y
0
I recommend keeping it in startergui and using it as a local script as it works more efficiently Prestory 1395 — 4y
0
Thanks bro! I appreciate your help! :D Hyperpublic 4 — 4y
0
NP, but next time please post a bit of code Prestory 1395 — 4y
Ad

Answer this question