How to detect a player stop holding down a key?

So, I am creating a breathing system for my game, The system works so far but I want to make it so when they stop holding G there will be a bit of a cooldown. How would I do this?

local Player = game.Players.LocalPlayer

local UIS = game:GetService("UserInputService")
local rp = game:GetService("ReplicatedStorage")

local debounce = false


UIS.InputBegan:Connect(function(Input,isTyping)
	if isTyping then
		return
	elseif UIS:IsKeyDown(Enum.KeyCode.G) then
		while UIS:IsKeyDown(Enum.KeyCode.G) do
			if debounce then
				wait(5)
				debounce = false
				return
			end
			
			--debounce = true 
			print("Breath")
			game.ReplicatedStorage.BreathEvent:FireServer()
			wait(1)
			
	

		
		end	
	end
end)


Use UIS.InputEnded. It gives you the same parameters as .InputBegan except it fires when you release focus on an input.

1 Like
local Player = game.Players.LocalPlayer

local UIS = game:GetService("UserInputService")
local rp = game:GetService("ReplicatedStorage")

local debounce = false

UIS.InputBegan:Connect(function(Input,isTyping)
	if isTyping then
		return
	elseif UIS:IsKeyDown(Enum.KeyCode.G) and debounce == false then
		debounce = true 
		while UIS:IsKeyDown(Enum.KeyCode.G) do
			print("Breath")
			game.ReplicatedStorage.BreathEvent:FireServer()
			task.wait(1)
		end	
		
		if debounce then
			task.wait(5)
			debounce = false
			return
		end
	end
end)

This code makes it so that once they stop holding G, they cannot hold it again for 5 seconds.

2 Likes