✓ Accepted answer
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
KeyBeingPressed = true
while KeyBeingPressed do
end
end
end
end
local function KeyUp(InputConcluded)
if InputConcluded.UserInputType == Enum.UserInputType.Keyboard then
if InputConcluded.KeyCode == Enum.KeyCode.Zero then
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.