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

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

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.

1 comment

0

Use userinputservice.

1 answer

✓ Accepted answer

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)

5 comments

0

this isnt a request site...oh well, i wonder how long its gonna take before you're banned

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?

0

I recommend keeping it in startergui and using it as a local script as it works more efficiently

0

Thanks bro! I appreciate your help! :D

0

NP, but next time please post a bit of code

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