I wanna know how to do this

I wanna know how i would make it so that you press a key for too long it will automaticly release the key. For example like i have my key set to f key and set to that i you hold the key it will do nothing but when you released the key it will print something, But i have the problem that you could hold the key for as long as you want , what i want do is that when you held the f key for like 3 seconds it will automaticly released the key and print something. If you happens to know how to do this please let me know,I can’t find any toturiols about this one. :slightly_smiling_face:

local start = tick()
repeat 

until not Holding or (tick() - start) >= 5
1 Like
local UIS = game:GetService('UserInputService')
local currentTick
local cooldown = false

UIS.InputBegan:Connect(function(input, gpe)
if gpe then return end

if input.KeyCode == Enum.KeyCode.F and cooldown == false then
spawn(function() local current = tick() repeat task.wait() until current >= 2 cooldown = false end)
currentTick = tick()
repeat wait() until timer >= 3 or currentTick == nil
currentTick = nil
-- do something
end
end)

UIS.InputEnded:Connect(function(input, gpe)
if gpe then return end

if input.KeyCode == Enum.KeyCode.F and currentTick ~= nil then
currentTick = nil
end
end)

ill use variable as tick so ill make sure when inputended is released

game:GetService("UserInputService").InputBegan:Connect(function(input, gp)
if gp then return end

if input.KeyCode == Enum.KeyCode.E then -- Change 'E' to the key
       if tick() >= 2 then -- Change '2' to how long you want to hold the key
       -- Do stuff
    end
end)

Sorry it dosen’t work for me, When i’m holding it dosen’t do anything it omly do something when i released the key.

game:GetService(“UserInputService”).InputBegan:Connect(function(input, gp)
if gp then return end

if input.KeyCode == Enum.KeyCode.E then -- Change 'E' to the key
	if tick() >= 2 then -- Change '2' to how long you want to hold the key
		print(":D")
	end
end

end)

I wrote thid and it just print no waiting at all

here my questions and notes here

  • Did you replace - do something to the thing you wanted to do it?
    – if yes what lines of code did you put after replacing -- do something?

notes:

  • Idk if is it possible to do a automatic release

I replace --do something with print(“F”)

did the error threw or just ignoring it?

No erors OOOOOOOOOOOOOOOOOOOOOOOOOO 30 Characters

I only gave you an example, now you gotta write the code where it says ‘Do stuff’

I also don’t even know what you want to do when holding it.

I want to make it so that when i hold the key for a 3 seconds it will print something

local UIS = game:GetService("UserInputService")
local Key = Enum.KeyCode.F
local KeyDown = false
local WaitTime = 3

function dosomething()
	warn("Hello")
end

UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Key then
		KeyDown = true
		spawn(function()
			local Time = os.time()
			repeat
				wait()
			until not KeyDown or os.time()-Time >= WaitTime
			dosomething()
		end)
	end
end)

UIS.InputEnded:Connect(function(key)
	if key.KeyCode == Key then
		KeyDown = false
	end
end)
local UIS = game:GetService("UserInputService")

local Duration = 3 -- held time

UIS.InputBegan:Connect(function(Input, GPE)
	if Input.KeyCode == Enum.KeyCode.E then -- change the enumarated keycode if 'E' keycode isnt the keycode for your code
		local Time = tick()		
		repeat
			if (tick() - Time) >= Duration then 
				print("success")
				-- do something if player held 'E' key for 3 seconds 
				break
			end
			task.wait()
		until
		not UIS:IsKeyDown(Input.KeyCode) 
		if (tick() - Time) < Duration  then
			print("failed")
			-- do something if player didnt held 'E' key for 3 seconds or just the player pressed it
		end
	end
end)

Thanks! it worked. :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: :sunglasses: