Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you do KeyHeld?

Asked by 10 years ago

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

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
10 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
--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)

Ad
Log in to vote
-1
Answered by 10 years ago

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.

Answer this question