Combat system damage not working properly v2

  1. I am trying to achieve a combat system where the damage works normally and only when you punch.

  2. if I punch and touch nothing and then touch someone it will damage them. As well if I punch and touch someone you can still damage him after punching

problem1
https://gyazo.com/85b7a5f49b280cf02573e34536bd3ac8

problem2
https://gyazo.com/acfcde9bd70b7ad2fd4c9a1433924dbd

  1. I have made a post about this before but the problem seems to be back since I modified the script a bit.
local remote = game.ReplicatedStorage.remote

local connection
local function hitStuff(p,player)
	local canDamage = true
	local rightArm = player.Character:FindFirstChild("Right Arm")
	local LeftLeg = player.Character:FindFirstChild("Left Leg")
	
	if canDamage == true then
		canDamage = false
		rightArm.Touched:Connect(function(hit)
			local h = hit.Parent:FindFirstChild("Humanoid")
			if h then
				h:TakeDamage(10)
				canDamage = false
				connection:Disconnect()
			end

		end)
	end
	wait(0.5)
	connection:Disconnect()
end

connection = remote.OnServerEvent:Connect(hitStuff)

You haven’t assigned connection, so there is nothing to disconnect.
replace

rightArm.Touched:Connect(function(hit)

with

connection = rightArm.Touched:Connect(function(hit)