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

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

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

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.

3 comments

1

You could also do keydown and keyup events, That probably works better

1

great answer

0

Thx bash:)

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