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

Example of a hold key script ??

please give me a 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.

1 comment

0

thanks

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