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

How do you do KeyHeld?

I am making a script that allows you to hold a key to fly up.

local mouse = game.Players:FindFirstChild(script.User.Value):GetMouse()
mouse.KeyDown:connect(function(key)
	if key:lower() == "q" then
		print("QKeyDown")
	end
end)

That is what I have so far, but you have to keep pressing the key to make it print. I want to make it so it continuously prints when you have the key held down. Is there a mouse.KeyHeld:connect method, or a different way of doing it?

2 answers

--Put in StarterGui or StarterPack

repeat wait() until game.Players.LocalPlayer
local p = game.Players.LocalPlayer
local m = p:GetMouse()
local down = false
math.randomseed(tick())

m.KeyDown:connect(function(key)
	if key:lower() == "a" then
		down = true
		repeat
			print(math.random(1337))
			wait()
		until 
		down == false
	end
end)

m.KeyUp:connect(function(key)
	if key:lower() == "a" and down then
		down = false
	end
end)

For printing continuously, do a 'while' loop.

while key:lower() == "q" do
	wait()
	print("QKeyDown")
end

However, since you're making the character fly up whilst holding down a key, you don't need to do that. KeyDown should work fine, even if the print function works differently. I'm not sure if that made sense. Probably didn't. What I'm trying to say is your script should work fine if you replaced the 'print' with your function.

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