Sword Damage and Sound not Working Properly

Hello, I am currently making a sword system where when you swing the sword, it damages the enemy and plays a sound. But the problem is that the sword is having some weird bugs, Like when you first swing the sword, it does damage and sound only when you swing the sword and sword touches the other player. But after the first swing, the sword plays sound and does damage even when you don’t swing your sword, but it doesn’t happen before your first swing.

Video:

I have tried everything, but nothing seems to be working. Here are my scripts. Sorry if they are unorganized and unoptimized, I am still new to roblox studio and is not the best at scripting

Script One:

local DebounceTable = {}
local tool = script.Parent.Parent

tool.Activated:Connect(function()
	script.Parent:WaitForChild("Part").Touched:Connect(function(objectThatTouchesTheHitbox)
		if objectThatTouchesTheHitbox.Parent then
			if objectThatTouchesTheHitbox.Parent:FindFirstChild("Humanoid") then
				if DebounceTable[objectThatTouchesTheHitbox.Parent] == true then return end
				DebounceTable[objectThatTouchesTheHitbox.Parent] = true
				objectThatTouchesTheHitbox.Parent.Humanoid:TakeDamage(30)
				wait(0.75)
				DebounceTable[objectThatTouchesTheHitbox.Parent] = false
			end
		end
	end)
end)

Script Two:

local tool = script.Parent
local hitbox = tool.Handle.Part
local animId = "rbxassetid://132940301793859"
local hitSoundId = "rbxassetid://935843979"  
local cooldownTime = 0.75 
local canActivate = true  

tool.Activated:Connect(function()
	
	if not canActivate then return end
	
	canActivate = false
	
	local plr = game.Players.LocalPlayer
	local character = plr.Character or plr.CharacterAdded:Wait()

	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid then
		
		local anim = Instance.new("Animation")
		anim.AnimationId = animId

		local animTrack = humanoid:LoadAnimation(anim)
		animTrack.Priority = Enum.AnimationPriority.Action4  
		animTrack:Play()
	end

	
	task.wait(cooldownTime)
	canActivate = true
end)

local hitSound = Instance.new("Sound")
hitSound.SoundId = hitSoundId
hitSound.Volume = 1 
hitSound.Parent = tool.Handle

tool.Activated:Connect(function()
	hitbox.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			print("Hit detected!")  

			if not canActivate then return end
			-- Start the cooldown
			canActivate = false

			print("Playing sound!")  
			
			hitSound:Play()

			
			task.wait(cooldownTime)
			canActivate = true
		end
	end)

end)

You see, there are multiple issues here, although I don’t know which one is causing the undesired behaviour of the animation not playing when prompted to.
I want first of all you to re-design your first code such that it can damage ONLY when the animation is playing. You are damaging the entity also without swinging because once the tool is activated, the sword becomes a kill part in automatic. You should do it in such a way that when the player swings the sword, so the animation is playing, that’s when the tool activates. Here you can find something that might help you.
About the animation… I am not sure, although I see you’re setting the priority to 4. Try and set it to 1.
Let me know if any of these help.

P.S. play the sound after the sword hit the humanoid, WITHOUT having 2 separate tool.Activated parts.

you can try this
local tool = script.Parent
local damage = 20 – Change this to increase/decrease sword damage
local hitSound = tool:FindFirstChild(“HitSound”)

tool.Activated:Connect(function()
local character = tool.Parent
local humanoid = character:FindFirstChild(“Humanoid”)
if not humanoid then return end

local rayOrigin = character:FindFirstChild("Head").Position
local rayDirection = character:FindFirstChild("Head").CFrame.LookVector * 5

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local result = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

if result and result.Instance then
	local hitHumanoid = result.Instance.Parent:FindFirstChild("Humanoid") or result.Instance.Parent:FindFirstChildWhichIsA("Humanoid")
	if hitHumanoid and hitHumanoid ~= humanoid then
		hitHumanoid:TakeDamage(damage)
		if hitSound then
			hitSound:Play()
		end
	end
end

end)

> Blockquote

1 Like