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
4 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 4 years ago

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

You could try something like this

01local UIS = game:GetService("UserInputService")
02UIS.InputBegan:connect(function(keycode)
03 
04if keycode.keyCode == Enum.KeyCode.w then -- Change w to the key you want
05    local keydown = 0
06    for rounds = 1,(5 * 4) do --change 5 to the amount of seconds you want
07        wait(0.25)
08        if keycode.keyCode == Enum.KeyCode.w then -- Change w to the key you want
09            keydown = keydown + 1
10        end
11        if keydown ~= rounds then
12            return
13        end
14    end
15    if rounds == keydown then
16        --Heres the script thats going to be executed after holding keydown
17    end
18end

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 — 4y
1
great answer BashGuy10 384 — 4y
0
Thx bash:) Skydoeskey 108 — 4y
Ad

Answer this question