Sword Damaging Multiple Times

Hello, I have a sword that deals damage on hit. This sword works on click or hold. This sword has two separate scripts, one to do the damage and one to plays animations for the sword. The sword works on hit, so if I hit an NPC, even if I am not swinging my sword, I can still damage them. Is there any way I could make the sword only work when I play the animation?

2 Likes

not entirely sure what the issue is because i dont see the code…
also, have a boolean to keep from spamming an enemy with damage…

You could make it so before you click teh damage script it disabled
so something like script.Parent.DamgeScript.Enabled = true and then make ti false when the animation is done
Also to make the sword damage only 1 time, you can make a debounce, which there aren many tutorials on

Heres the script:

tool.Activated:Connect(function(attack)
	
	if debounce then return end
	debounce = true
	
	tool:WaitForChild("Parts").Blade.Touched:Connect(function(hit)
		
		if hit.Parent:FindFirstChild("Humanoid") then  -- Will run if the item that touched the handle has a Humanoid
			
			local humanoid = hit.Parent.Humanoid
			local highlight = hit.Parent:WaitForChild("Highlight")
			
			
			humanoid:TakeDamage(15)
			print("dmg")
			highlightFunction(highlight)
			
			
			local Creator_Tag = Instance.new("ObjectValue")
			Creator_Tag.Name = "creator"
			Creator_Tag.Value = script.Parent.Parent.Parent
			Debris:AddItem(Creator_Tag, 2)
			Creator_Tag.Parent = humanoid
			
		end
	end)
	
	task.wait(tool:GetAttribute("Cooldown1"))
	debounce = false
end)

With your script I made this.
Not perfect but it should do for now.

Client

local tool = script.Parent
local debounce = false
local startTime = tick()

tool.Activated:Connect(function(attack)
	if debounce then 
		return 
	end
	debounce = true

	tool:WaitForChild("Handle").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local humanoid = hit.Parent.Humanoid
			--local highlight = hit.Parent:WaitForChild("Highlight")

			game:GetService("ReplicatedStorage").DamagedEvent:FireServer(humanoid)
			--highlightFunction(highlight)
		end
	end)

	local cooldown = tool:GetAttribute("Cooldown1") or 0
	if cooldown > 0 then
		while tick() - startTime < cooldown do
			task.wait(0.1)
		end
	end

	debounce = false
end)

Server

local replicatedStorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")

replicatedStorage.DamagedEvent.OnServerEvent:Connect(function(player, humanoid)
	humanoid:TakeDamage(5)
	
	local tag = Instance.new("ObjectValue")
	tag.Name = "Creator:"..player.UserId
	tag.Value = script.Parent
	debris:AddItem(tag, 2)
	tag.Parent = humanoid
end)

Screenshot
image