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

how to detect if player is holding a certain key for a certain time?

Asked by
Grazer022 128
3 years ago

I wanna know how to detect the user/player is holding down a certain key for let’s say 5 seconds. Thanks!

also should it be in a server or local script? Thanks again!

1 answer

Log in to vote
1
Answered by 3 years ago

It should be local script because key detection is mainly used locally.

You could try something like this

local UIS = game:GetService("UserInputService")
UIS.InputBegan:connect(function(keycode)

if keycode.keyCode == Enum.KeyCode.w then -- Change w to the key you want
    local keydown = 0
    for rounds = 1,(5 * 4) do --change 5 to the amount of seconds you want
        wait(0.25)
        if keycode.keyCode == Enum.KeyCode.w then -- Change w to the key you want
            keydown = keydown + 1
        end
        if keydown ~= rounds then
            return
        end
    end
    if rounds == keydown then
        --Heres the script thats going to be executed after holding keydown
    end
end

I haven't tested if this works. And there might be a better way.

1
You could also do keydown and keyup events, That probably works better Skydoeskey 108 — 3y
1
great answer BashGuy10 384 — 3y
0
Thx bash:) Skydoeskey 108 — 3y
Ad

Answer this question