please give me a example of a hold key script ??
✓ ANSWER ACCEPTED
Example of a hold key script ??
1 answer
✓ Accepted answer
Just use an InputEnded function with UserInputService
local userInputService = game:GetService("UserInputService")
userInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Q then-- Example
print("Was holding Q!")
end
end)
Now for a timed one:
local userInputService = game:GetService("UserInputService")
local holdingTime = 0
local stillHoldingQ = false
userInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Q then-- Example
while stillHoldingQ == true do
wait(1)
holdingTime = holdingTime + 1
end
end
end)
userInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Q then-- Example
stillHoldingQ = false
print("Was holding Q for "..holdingTime.." seconds!")
holdingTime = 0
end
end)
This works by detecting different input changes with the UserInputService class.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
thanks