Holding Input Script

  1. What do you want to achieve?
    I’m trying to make a script where you hold down a button and after a certain time it’ll reset or if you release early it’ll reset too.
  2. What is the issue?
    The script resets but if you let go early it’ll print it’s still holding a little, not sure why

here you can see if you release input early it will still print holding, but if you hold until it resets that’s fine and it works, it resets instead of going to release
https://gyazo.com/dbd6bd6e5ea4dc4f29c3da5362a19fec

local script

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local mouse = player:GetMouse()
local debounce = false

local uips = game:GetService("UserInputService")

uips.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		if debounce == true then return end
		if not debounce then
			debounce = true
			
			local starttime = os.clock()
			
			while debounce do
				task.wait()
				
				print("holding")
				
				if os.clock() - starttime >= 4 then
					print("reset")
					debounce = false
				end
			end
			starttime = os.clock()
			
		end
	end
end)

uips.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		if not debounce then return end
		debounce = false
		print("release")
	end
end)

Do you really need os.clock? If not just use for loop.

i need to measure the time difference so it can timeout?

i don’t know if this is what you’re going for but here you go

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local uips = game:GetService("UserInputService")

local debounce = false
local isHolding = false

uips.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F and not debounce then
		debounce = true
		isHolding = true
		local startTime = os.clock()
		print("holding")

		while isHolding do
			if os.clock() - startTime >= 4 then
				print("reset")
				isHolding = false
				debounce = false
				break
			end
			task.wait()
		end
	end
end)

uips.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F and debounce then
		isHolding = false
		debounce = false
		print("release")
	end
end)

just add the GetMouse() if you need it

os.clock() is fine to use here

1 Like

thank you, I changed some things to make my code similar with yours, I think it was the task.wait() that fixed it

the code if anyone wants it, God bless

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local mouse = player:GetMouse()
local debounce = false

local uips = game:GetService("UserInputService")

uips.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F and not debounce then
		debounce = true
		
		local starttime = os.clock()
		
		while debounce do
			print("holding")
			
			if os.clock() - starttime >= 4 then
				print("reset")
				debounce = false
				break
			end
			task.wait()
		end
		starttime = os.clock()
		
	end
end)

uips.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F and debounce then
		debounce = false
		print("release")
	end
end)
3 Likes