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.
How to detect if a player is holding down a button (eg.: R) on keyboard?
1 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
this isnt a request site...oh well, i wonder how long its gonna take before you're banned
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?
I recommend keeping it in startergui and using it as a local script as it works more efficiently
Thanks bro! I appreciate your help! :D
NP, but next time please post a bit of code
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Use userinputservice.