Player gets damage when animation isn't playing

I made a roblox punch script and it works
but when the player arm touches a player or npc it does damage even though the animation isn’t playing,
there is no error in the output
how can I fix this?

UIS.InputBegan:Connect(function(key)
	if key.keyCode == Enum.UserInputType.MouseButton1 or Enum.KeyCode.F then
		PunchAnim:Play()
		if PunchAnim.IsPlaying then
			script.Parent["Right Arm"].Touched:Connect(function(hit)
				if hit.Parent.Humanoid and Dmg == false then
					hit.Parent.Humanoid:TakeDamage(20)
					Dmg = true
					wait(1)
					Dmg = false
					end
			end)
		end
	end
end)

Full Script

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local Humanoid = player.Character.Humanoid
local Dmg = false
local debounce = false

local Punch = script:WaitForChild("Punch")
local PunchAnim = Humanoid:LoadAnimation(Punch)

local Kick = script:WaitForChild("Kick")
local KickAnim = Humanoid:LoadAnimation(Kick)

UIS.InputBegan:Connect(function(key)
	if key.keyCode == Enum.UserInputType.MouseButton1 or Enum.KeyCode.F then
		PunchAnim:Play()
		if PunchAnim.IsPlaying then
			script.Parent["Right Arm"].Touched:Connect(function(hit)
				if hit.Parent.Humanoid and Dmg == false then
					hit.Parent.Humanoid:TakeDamage(20)
					Dmg = true
					wait(1)
					Dmg = false
					end
			end)
		end
	end
end)

cause you’re not disconnecting the connection/signal.

UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.UserInputType.MouseButton1 or key.KeyCode == Enum.KeyCode.F then
		PunchAnim:Play()
		if PunchAnim.IsPlaying then
            local Connection
			Connection = script.Parent["Right Arm"].Touched:Connect(function(hit)
				if hit.Parent.Humanoid and Dmg == false then
					hit.Parent.Humanoid:TakeDamage(20)
					Dmg = true
					wait(1)
                    Connection:Disconnect()
					Dmg = false
					end
			end)
            wait(1)
            Connection:Disconnect()
		end
	end
end)
2 Likes

Doesn’t really work
It still does damage to the npc when the arm touches and the animation not playing

try it again i edited it, i think it was caused by your statement not being specified.

2 Likes

Nvm I got it to work
I had to remove the Enum.UserInputType.MouseButton1
Thanks for the help!

i Edited it again turns out i forgot to make it disconnect when it doesn’t hit anything.

1 Like