How to hold a button down

So, currently once I hold down my F button, immediately the block script will unblock it. The script doesn’t even get to work, so I’d like just a little bit of help.

UIS.InputBegan:Connect(function(Input, IsTyping)
	if not IsTyping then
		if Input.KeyCode == Enum.KeyCode.F and Debounce == false then 
			Debounce = true
			
			RS.Block:FireServer()
			print("It work")
			Blockin:Play()
			while ButtonDown == true do
				wait()
			
			end		
			print("It no work")
			Blockin:Stop()
			RS.Release:FireServer("Release")
			wait(.9)
			Debounce = false
		end
	end
end)

2 things.

  1. I don’t see you ever setting “ButtonDown” to true
  2. You need to use InputEnded to detect when it ends.
UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		ButtonDown = false
	end
end)
2 Likes

would I need to make the variables global in that case?

Yes, you would need to make the variable global so it can be changed outside of the function.

UIS.InputBegan:Connect(function(Input, IsTyping)
	if not IsTyping then
		if Input.KeyCode == Enum.KeyCode.F and Debounce == false and ButtonDown == false then 
			Debounce = true
			
			RS.Block:FireServer()
			print("It work")
			Blockin:Play()
			while ButtonDown == true do
				wait()
			
			end
		end
	end
end)

UIS.InputEnded:Connect(function(Input, IsTyping)
	if not IsTyping then
		if Input.KeyCode == Enum.KeyCode.F and Debounce == false and ButtonDown == true then
			print("It no work")
			Blockin:Stop()
			RS.Release:FireServer("Release")
			wait(.9)
			Debounce = false
		end
	end
end)

I changed it to this, but the input never changes, and only the first function works.

1 Like

Sorry, for the late reply, but you never set the debounce to false in either function. Also, wait() throttles, so use task.wait().

1 Like