How to Prevent Multiple Sound Playing on Hit

So, I am making this simple slash and hit weapon stick that has sound effects when you hit a humanoid (Except Player).

My problem is when it is already hitting a humanoid model, the touch event fires multiple at the same time.

I know how debounce works but the “Thud” sound should be only possible if the hitting animation is at the slashing animation.

Is there a way to limit to 1 hit only per slash?

Here’s my code:

local AnimationTrack
Tool.Activated:Connect(function()
	if Humanoid and Humanoid.Health > 0 then
		if AnimationTrack == nil then
			local Animator = Humanoid.Animator
			local Animation= Instance.new("Animation")
			Animation.AnimationId = "rbxassetid://" .. Animations.Slash_1
			
			AnimationTrack = Animator:LoadAnimation(Animation)
			AnimationTrack:Play()
			
			
			Slashing = true
			
			-- Slash
			local Slash = SFX.Slash[math.random(#SFX.Slash)]
			Slash:Play()
			
			local HitDebounce = false
			Handle.Touched:Connect(function(hit)
				local isUser = Players:GetPlayerFromCharacter(hit.Parent)
				if hit.Parent:FindFirstChildWhichIsA("Humanoid") and isUser ~= LocalPlayer then
					if Slashing == true and HitDebounce == false then
						HitDebounce = true
						local HitThud = SFX.Thuds[math.random(#SFX.Thuds)]
						HitThud:Play()
						HitDebounce = false
						HitThud.Ended:Wait()
					end
				end
			end)
			
			
			Trail.Enabled = true
			AnimationTrack.Stopped:Connect(function()
				Trail.Enabled = false
				Slashing = false
				task.wait(.25)
				AnimationTrack = nil
			end)
		end
	end
end)
1 Like

Couple things. First, you need to disconnect your connection. Currently, everytime you slash you’re creating a new Touched connection which is going to cause chaos. Also, If you’re only interested in the thud playing one time per slash, you want your debounce to go false only at the end of the animation. This ought to work:

local AnimationTrack
Tool.Activated:Connect(function()
	if Humanoid and Humanoid.Health > 0 then
		if AnimationTrack == nil then
			local Animator = Humanoid.Animator
			local Animation= Instance.new("Animation")
			Animation.AnimationId = "rbxassetid://" .. Animations.Slash_1

			AnimationTrack = Animator:LoadAnimation(Animation)
			AnimationTrack:Play()
			
			Slashing = true

			-- Slash
			local Slash = SFX.Slash[math.random(#SFX.Slash)]
			Slash:Play()
			
			local hitDebounce = false
			
			local hitConn; hitConn = Handle.Touched:Connect(function(hit)
				if hitDebounce then return end
				hitDebounce = true
				
				local isUser = Players:GetPlayerFromCharacter(hit.Parent)
				
				if hit.Parent:FindFirstChildWhichIsA("Humanoid") and isUser ~= LocalPlayer then
					if Slashing then
						hitConn:Disconnect()
						
						local HitThud = SFX.Thuds[math.random(#SFX.Thuds)]
						HitThud:Play()
						HitThud.Ended:Wait()
					end
				end
				
				hitDebounce = false
			end)


			Trail.Enabled = true
			AnimationTrack.Stopped:Connect(function()
				hitConn:Disconnect()
				hitDebounce = false
				
				Trail.Enabled = false
				Slashing = false
				task.wait(.25)
				AnimationTrack = nil
			end)
		end
	end
end)
3 Likes

You need to add some time instead of just wait().

HitThud.Ended:Wait(1) will add a 1 second pause for hit sound. Just change the value to what works for you.

edit Hehe, @Ourwbwrws beat me to it and did it way better.

1 Like