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

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

Asked by 8 years ago

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.

0
Use UserInputService InputBegan to indicate what button is being pressed, then use InputEnded to indicate if that button has been released. M39a9am3R 3210 — 8y
0
Can you give me a example? InfinitivePixelsJr 542 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

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
Ad

Answer this question