How would I go about doing something that will show how close I am to executing something, let’s say, how close I am to fully opening a door, while holding down a specific button, like “E”.
I usually have a loop going on in the background, and if someone holds E or such, the loop is already adding percentage to the bar a couple times a second until it reaches 100.
Is there any other way I can do this?
Use a while loop and a debounce:
local UIS = game:GetService("UserInputService") local KeyBeingPressed = false local function KeyDown(InputStarted) if InputStarted.UserInputType == Enum.UserInputType.Keyboard then if InputStarted.KeyCode == Enum.KeyCode.Zero then -- Replace "Zero" with the key you want KeyBeingPressed = true while KeyBeingPressed do -- Do something here end end end end local function KeyUp(InputConcluded) if InputConcluded.UserInputType == Enum.UserInputType.Keyboard then if InputConcluded.KeyCode == Enum.KeyCode.Zero then -- Same thing here as above KeyBeingPressed = false end end end UIS.InputBegan:Connect(KeyDown) UIS.InputEnded:Connect(KeyUp)
The debounce will end the while loop when it becomes false (when the key being held down is no longer being held down), which means that the while loop will not last forever. For the credit, the script I posted above is a LocalScript
because the UserInputService
can only be used in LocalScripts
.