How do I debounce a punch ability correctly?

  1. What do you want to achieve? Keep it simple and clear!
    I want to correctly debounce a punch ability
  2. What is the issue? Include screenshots / videos if possible!
    The punch works on touch, when I press the R key, although, after I press R key, even when the punch has ended, it will still do damage to whatever I touch
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried countlessly looking for different tutorials on this matter. Although, none seem to work…

Part of my code:

elseif  input.KeyCode == Enum.KeyCode.R and not istyping then
		if CanDoDamage == true then
			
			CanDoDamage = false
			local anim = hum:LoadAnimation(script.DonutAnim)
			anim:Play()
			hum.Parent:FindFirstChild("Right Arm").Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") then
					hit.Parent.Humanoid:TakeDamage(50)
					CanDoDamage = false
					wait(1.5)
					CanDoDamage = true
				end
			end)
		end

When lua codes reach a function, it always keeps that code open, it may be necessary to check the debounce part in touched, you can do it like this

hum.Parent:FindFirstChild("Right Arm").Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") and CanDoDamage then
					hit.Parent.Humanoid:TakeDamage(50)
					CanDoDamage = false
					wait(1.5)
					CanDoDamage = true
				end
			end)

hope this helps :smiley:

says
Always call Disconnect() when a connection is no longer needed. Forgetting to do so can cause your game to use more resources than necessary. Note, however, that this may not always be necessary; when an object is destroyed, all connections to that object’s events are disconnected automatically.