Help With Punch Script

So. I have been trying to code a punch script. All works except when i punch a player, after that i can just run into then and they take damage. Btw, this is just a localscript for now, ill change it later.

local function damage()
	game.Players.LocalPlayer.Character:WaitForChild("Humanoid").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if damagedb == true then
				script.Hit1:Play()
				damagedb = false	
				hit.Parent.Humanoid:TakeDamage(5)
				print(hit)
				wait(1)
				damagedb = true
			end
		end
	end)
end

1 Like

Where is your whole script? What is exactly the problem you are encountering? There’s not enough information to solve whatever problem you are trying to convey.

The .Touched event stays connected even after the code runs. That means the code inside of the function will run every time something touches the character. If you want to stop .Touched from firing, use Connection:Disconnect()

local connection = humanoid.Touched(function(hit) 
    -- blah
end)

-- when you want to disconnect the event
connection:Disconnect()

You could disconnect the event after a certain amount of time has passed, or after you hit something.
P.S. I would recommend creating a hitbox that uses the .Touched event instead, and using the hit part from the character to get the humanoid you need to damage.

once a humanoid is punched, the puncher can freely run into that humanoid and deal damage without needing to punch.
here is my whole script.

local db = true
local damagedb = true

local function damage()
	game.Players.LocalPlayer.Character:WaitForChild("Humanoid").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if damagedb == true then
				script.Hit1:Play()
				damagedb = false	
				hit.Parent.Humanoid:TakeDamage(5)
				print(hit)
				wait(1)
				damagedb = true
			end
		end
	end)
end

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://14404521983​"

game:GetService("UserInputService").InputBegan:Connect(function(pressed)
	if pressed.KeyCode == Enum.KeyCode.F and db == true then 
		db = false
		local anim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(animation)
		anim:Play()
		damage()
		wait(1.3)
		db = true
	end
end)

`