Sword damage doesn't work

Hello, I’m a beginner, and I’m scripting a sword with custom animation when equipped and when you walk. But also, when you hit any npc/user it will run an animation of getting sliced.

The attack animation work perfectly, but when I attack with it, it gives me an error (the damage doesn’t works, the “sliced” animation doesn’t actives). and I have no idea how to fix it.

However, sometimes it does damage and the sliced animation works. (Usually at exact positions or when you attack while jumping) And when it does works, the error doesn’t appear.

My thoughts is that It may be something related to hitbox but I’m not sure because of the error.

function onTouch(hit)
	
	script.Disabled = true
	
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	
	local animation = script.Animation
	
	local animationtrack = humanoid:LoadAnimation(animation)--This is the line that gives error

	animationtrack:Play()
	
	humanoid.Health = humanoid.Health - 10
	
end
script.Parent.Touched:Connect(onTouch)	

This is how the sword is structured, so you can get a better idea overall.

Structure1

The red lines indicates wich script runs when attacking and the animation that it uses.

humanoid is nil, try doing

	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	
	if not humanoid then return end -- Will return if humanoid doesn't exist

	local animation = script.Animation
	
	local animationtrack = humanoid:LoadAnimation(animation)--This is the line that gives error

	animationtrack:Play()

Side Note: Humanoid:LoadAnimation() is deprecated, use Humanoid.Animator

Hello, thanks for your quick response.

This appears when I use it:

(Also, script error says that you missed a then, I added it)

if not humanoid then
return end

1 Like
local animationtrack = humanoid:LoadAnimation(animation)--This is the line that gives 

Try without I guess?

1 Like

The code @acuaro sent works, it’s just the dummy does not have an animator object in humanoid.

Try the code on a player character.

2 Likes

Thanks It does works!, now I’ll have to figure out how to use debounce or something like that on it because it does constant damage (but that is out of the original problem that you were answering, thank you!)

1 Like