My punch animation plays but doesnt do damage

Hello!

The title pretty much explains itself, haha

But me and my friend Shonny_Yeet created a script for a right punch, the animation plays but the damage script doesnt seem to be running even though there are no errors in the scripts output

heres the script

--Local stuff
local tool = script.Parent
local CoolDown = script.Cooldown.Value
local animation = script.Animation --Put the name of the animation here!


--Config
--You can change Cooldown.
tool.Activated:Connect(function()
	local humanoid = tool.Parent:FindFirstChildWhichIsA('Humanoid')
	if humanoid and CoolDown == false then
		CoolDown = true
		local playanim = humanoid:LoadAnimation(animation) 
		print("Working 2!")
		playanim:Play() 
		print("Working 3!")
		wait(0.5)
		playanim:Stop()
		print("Working 4!")
		wait(0.4)
		print("Working 5!")
		CoolDown = false
		print("Working 6!")
		--Damage Script Below
		local damage = (10) --damage amount
		local player = game.Players.PlayerAdded --the player
		local character = player.Character or player.CharacterAdded()	--the character	
		tool.Activated:Connect(function() --Connecting the damaging script
			local touchingParts = character["Right Arm"]:GetTouchingParts()
			for i,v in ipairs(touchingParts) do --looping through the table
				print(i..v)
				local OtherHumanoid = tool.Parent.Humanoid
				OtherHumanoid:TakeDamage(damage)
			end
		end)
	end
end)

The damaging script was created by @astrozzyz , they allowed me to use it and i made some slight changes

When i play the game, this is what the output looks like
bM8rmliyBo

and here’s the actual game
mpKcPmspJM

The dummy im hitting wont take damage and im not so sure why, ive barely started coding and im confused on how i can fix this problem :sweat_smile:

--Local stuff
local tool = script.Parent
local CoolDown = script.Cooldown.Value
local animation = script.Animation --Put the name of the animation here!
local damage = 10
local player = game.Players.LocalPlayer
local character = player.Character

local connection = nil

--Config
--You can change Cooldown.
tool.Activated:Connect(function()
	local humanoid = tool.Parent:FindFirstChildWhichIsA('Humanoid')
	if humanoid and CoolDown == false then
		CoolDown = true
		local playanim = humanoid:LoadAnimation(animation) 
		print("Working 2!")
		playanim:Play()
		connection = character["Right Arm"].Touched:Connect(function(h)
				if h.Parent:FindFirstChildOfClass("Humanoid") then
				local OtherHumanoid = h.Parent.Humanoid
				OtherHumanoid:TakeDamage(damage)
			end
		end)
		print("Working 3!")
		wait(0.5)
		playanim:Stop()
		print("Working 4!")
		connection:Disconnect()
		wait(0.4)
		print("Working 5!")
		CoolDown = false
		print("Working 6!")
			
	end
end)

should work

Would this work even if im not using a local script?

Right, if you’re not using a localscript you can do
player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)

assuming you’re using a script in a tool

One more thing, haha

is the h. a variable for something? i dont really understand the h

h is just the hit val of the Touched event

Yeah, basically. It’s a variable created by the event

script.Parent.Touched:Connect(function(h) -- see that?

end)
1 Like

What does “OfClass” Mean? im trying to understand as much of the script as possible

A Class is what kind of thing the object is. For example, a Union’s class is “UnionOperation”. A part’s class is “Part”, ClassNames are unique, and can’t be changed, unlike normal Names.

FindFirstChildOfClass looks for the ClassName unlike FindFirstChild which finds the Name.

1 Like

Thank you!

But when i run the script, the output looks like this

here’s the updated script

Can you show lines 1-36 not 7-36?

here you go!

The player variable is gotten when it is in the backpack, not in the character, since it’s not in the Activated function. Move it into your Activated function.

would i move each variable into the activated function?

No, just player for now, unless one relies on player, such as character.

Thank you! It works now

But it works a bit too well, haha

here’s the result
9Zl2qU0jfr

i think the problem was using

Touched.Connect()

but im not so sure how to fix the problem, haha

here’s the update script, again

Add debounce to the hit, basically a hit cooldown so the damage isn’t constant.

It looks like you have an actual connection to the .Touched event (connection = character["Right Arm"].Touched:Connect(function(h)). Maybe make it so, if you want to hit one person only instead of multiple, when you hit someone disconnect the function like this connection:Disconnect(). If you want to keep multiple hits then disconnect it at the end of the script (when you set the CoolDown to false basically). If you don’t disconnect it, it will keep damaging other players even when the animation stopped and the CoolDown is set to false.

@DEVLocalPlayer

Thank you!

can you please show me an example? i still dont understand

Sure!
If you didn’t understand the connection thing, if you look at line 21 there’s a connection = character… variable, which defines the .Touched function. You can use that to disconnect the Touched function and actually stop detecting hits. To do so, you can use connection:Disconnect().
Let’s completely remove the damage when the animation stops. At line 30, you can put the same thing as before, connection:Disconnect(). This will allow you to stop detecting hits after the animation has stopped.
But right now, since we’re disconnecting it only after the animation has stopped, it will keep damaging humanoids, even the ones it has already hit. To fix this, we have 2 choices:

  1. Damage multiple times, but not the already hit players. To do so, we can use tables.
    local alreadyHit = {}
    When the Humanoid of a player has been damaged, you can use table.insert(alreadyHit,OtherHumanoid)
    And to actually prevent the damage to the already hit humanoids, you’ll have to put an if statement to check it before the OtherHumanoid is damaged.
local OtherHumanoid = h.Parent.Humanoid
if table.find(alreadyHit, OtherHumanoid) then
       return
end
OtherHumanoid:TakeDamage(10)
  1. Completely stop damaging other humanoids when one is hit. We can use only a variable, or a debounce, for this.
    local hasHit = false
    Then check if there already was a hit (in the Touched function) with
if hasHit then
   return
end
local OtherHumanoid = h.Parent.Humanoid
OtherHumanoid:TakeDamage(10)

I’d show you better if I had your script written instead of an image, but I hope I gave the idea on how to do it.

1 Like