Touched event doesn't work with an animation

Hi,
I made a sword but there is a problem : the “Touched” event doesn’t work when my animation is playing, and the sword goes through the Dummy.
I’ve tested with others animations and the script works but I don’t know why it don’t works with this one.

Video :

Script :

local sword = script.Parent
local Attack = script.Parent:WaitForChild("Attack")
local handle = script.Parent:WaitForChild("Handle")
local character
local trail = handle.Trail

local combo = 1
local canDamage = false
local cooldown = false
local cooldownTime = .5
local debounce = false
local Equipped = false
local countForReset = 0
local timeBeforeReset = 4
local hitDebounce = false

local AnimIdle = handle.Idle
local AnimWalk = handle.Walking
local AnimUnsheate = handle.Unsheate
local AnimSheate = handle.Sheate
local AnimSlash1 = handle.Slash1
local AnimSlash2 = handle.Slash2
local AnimSlash3 = handle.Slash3
local AnimSlash4 = handle.Slash5
local AnimSlash5 = handle.Slash5
local IdleAnimTrack
local WalkingAnimTrack
local AnimTrack 
local UnsheateAnimTrack
local SheateAnimTrack

local waitTime = 1

script.Parent.Equip.OnServerEvent:Connect(function(plr)
	
	character = plr.Character
	Equipped = true
	
	if SheateAnimTrack then
		if SheateAnimTrack.IsPlaying == true then
			SheateAnimTrack.Stopped:Wait()
		end
	end
	

	character.HumanoidRootPart.Anchored = true
	UnsheateAnimTrack = character.Humanoid:LoadAnimation(AnimUnsheate)
	UnsheateAnimTrack:Play()
	UnsheateAnimTrack.Stopped:Wait()
	character.HumanoidRootPart.Anchored = false

end)


sword.Unequipped:Connect(function()
	
	Equipped = false
	
	SheateAnimTrack = character.Humanoid:LoadAnimation(AnimSheate)
	character.HumanoidRootPart.Anchored = true
	SheateAnimTrack:Play()
	SheateAnimTrack.Stopped:Wait()
	character.HumanoidRootPart.Anchored = false
	if IdleAnimTrack then
		IdleAnimTrack:Stop()
	end
	
	if WalkingAnimTrack then
		WalkingAnimTrack:Stop()
	end
end)

script.Parent.StartRun.OnServerEvent:Connect(function()
	
	if Equipped == true then
		
		if IdleAnimTrack then
			IdleAnimTrack:Stop()
		end
		 
		if WalkingAnimTrack == nil then
			WalkingAnimTrack = character.Humanoid:LoadAnimation(AnimWalk)
			WalkingAnimTrack:Play()
			WalkingAnimTrack:AdjustSpeed(2)
			
		elseif WalkingAnimTrack.IsPlaying == false then
			WalkingAnimTrack = character.Humanoid:LoadAnimation(AnimWalk)
			WalkingAnimTrack:Play()
			WalkingAnimTrack:AdjustSpeed(2)

		end
		
	end
end)

script.Parent.EndRun.OnServerEvent:Connect(function()

	if Equipped == true then
		
		if WalkingAnimTrack then
			WalkingAnimTrack:Stop()
		end
		
		if IdleAnimTrack == nil then
			IdleAnimTrack = character.Humanoid:LoadAnimation(AnimIdle)
			IdleAnimTrack:Play()
		elseif IdleAnimTrack.IsPlaying == false then
			IdleAnimTrack = character.Humanoid:LoadAnimation(AnimIdle)
			IdleAnimTrack:Play()
		end
		
	end
   
end)

handle.Touched:Connect(function(hit)
	print(canDamage)
	if canDamage == true then
		if hit.Parent:FindFirstChildOfClass("Humanoid") then
			if hit.Parent ~= character then
				canDamage = false
				hit.Parent.Humanoid:TakeDamage(20)
			end
		end
	end
end)

sword.Activated:Connect(function()

	if debounce == false then
		debounce = true
		if cooldown == false then
			
			if WalkingAnimTrack then
				WalkingAnimTrack:Stop()
			end
			
			if IdleAnimTrack then
				IdleAnimTrack:Stop()
			end
			
			
			canDamage = true
			trail.Enabled = true
			character.HumanoidRootPart.Anchored = true
			
			if combo == 1 then
				
				AnimTrack = character.Humanoid:LoadAnimation(AnimSlash1)
				AnimTrack:Play()
				wait(waitTime)
				countForReset = 0
				combo = 2
				
			elseif combo == 2 then

				AnimTrack = character.Humanoid:LoadAnimation(AnimSlash2)
				AnimTrack:Play()
				wait(waitTime)
				countForReset = 0
				combo = 3
				
			elseif combo == 3 then

				AnimTrack = character.Humanoid:LoadAnimation(AnimSlash1)
				AnimTrack:Play()
				wait(waitTime)
				countForReset = 0
				combo = 4

				
			elseif combo == 4 then
				
				AnimTrack = character.Humanoid:LoadAnimation(AnimSlash2)
				AnimTrack:Play()
				wait(waitTime)
				countForReset = 0
				combo = 5

				
			elseif combo == 5 then
				
				AnimTrack = character.Humanoid:LoadAnimation(AnimSlash5)
				AnimTrack:Play()
				wait(2.2)
				countForReset = 0
				combo = 1
			end
		end
		
		wait(waitTime)
		trail.Enabled = false
		character.HumanoidRootPart.Anchored = false
		debounce = false
		canDamage = false
		IdleAnimTrack:Play()
	end
end)


while wait(1) do
	
	if countForReset < timeBeforeReset then
		countForReset += 1
	else
		combo = 1
		countForReset = 0
	end
end

(I know there are some useless variables but I don’t think they are the problem)

Please tell me if you have any idea about why this happens

1 Like

Where do I need to add :Connect(function() ?

I don’t know if i understand you well, because in there is already :Connect(function(hit) in the Touched part of my code :

handle.Touched:Connect(function(hit)
	print(canDamage)
	if canDamage == true then
		if hit.Parent:FindFirstChildOfClass("Humanoid") then
			if hit.Parent ~= character then
				canDamage = false
				hit.Parent.Humanoid:TakeDamage(20)
			end
		end
	end
end)

Sorry if you are not talking about this part of my script

Humanoid:LoadAnimation is deprecated.You can use Animator:LoadAnimation(animationTrack)

You can refer to the link above for more info about what im saying

Thank you, I will try this and tell you if it work !

1 Like

I would recommend adding a custom hitbox for the sword, rather than using handle itself. This can be accomplished simply by creating a part of adequate size, and welding it to the handle. Use this part for the touch event.

Instead of having a forever open touch event, you should create the touch event on the Tool.Activated portion, disconnecting the event when the sword is done slashing. This makes it so that the sword would only deal damage while it is being swung, rather than constantly. Although realistic, it seems weird in roblox for swords to do damage just always when you touch them.

Hope all that made sense. Good luck!

1 Like

Can u please tell what was printed here?

If i touched the dummy without activating the sword it printed false and if the animation was playing it didn’t print anything

its because u have kept CanDamage = false as the variable, but i have seen that you have typed
CanDamage = true on the activated Event and it will remain true only for that event. Try making a BoolValue inside the tool and change the variable to

Local CanDamage = script.Parent.BoolValueName

also change ever “Candamage” to “CanDamage.Value”

Hope it has helped you :slight_smile: .

1 Like

I found the problem !
The problem was that my animation was too quick and the sword didn’t touch the dummy !
That’s why it worked with others animations but not with these.
I’ll play a bit with AnimationTrack:AdjustSpeed() or change my animations and I think it will resolve the problem.
Thanks to everyone for your help !

1 Like