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

Any help with KeyHold?(I know it's not a function)

I was wondering if there's a function like KeyHold, kind of like KeyDown but for when you're holding a key.

Example:

local Player = game.Players.LocalPlayer
Player:GetMouse().KeyHold:connect(function(keydown)
	local key = string.lower(keydown)	
	if key == "c" then
		print(math.random(1,9999))
	end
end)

When I tried using KeyHold it gave me this error.

KeyHold is not a valid member of PlayerMouse

Any way I can achieve this? Any help appreciated.

Sorry I can't explain any better.

2 comments

0

Use UserInputService InputBegan to indicate what button is being pressed, then use InputEnded to indicate if that button has been released.

0

Can you give me a example?

1 answer

✓ Accepted answer

You can easily achieve this with a debounce, like so:

local mouse = game.Players.LocalPlayer:GetMouse()
local holding = false

mouse.KeyDown:connect(function(key)
	key = key:lower()
	if key == "f" then
		holding = true
	end
end)

mouse.Up:connect(function(key)
	key = key:lower()
	if key == "f" then
		holding = false
	end
end

while holding == true do
	-- code
end

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