Help on sword damage on activation

I want my sword to deal damage upon it is activated, instead I have a sword that just deals damage if something touches it, any ideas how to make this.
(I have 2 scripts, a local script and a script inside the tool)

script

local tool = script.Parent
local canAttack = true
local damage = math.floor(math.random(4,8))
local function onTouch(hit)

	local humanOther = hit.Parent:FindFirstChild("Humanoid")
	if not humanOther then return end
	if not canAttack then return end
	if humanOther.Parent == tool then return end
	humanOther:TakeDamage(damage)

	canAttack = false
	wait(0.3)
	canAttack = true

	tool.Handle.Touched:Connect(hit)
end


Local script

wait()
local player = game:GetService("Players").LocalPlayer
local character = player.Character
local animation1 = script.Parent:FindFirstChild("Attack 1")
local swingAnimation1 = character.Humanoid:LoadAnimation(animation1)

local animation2 = script.Parent:FindFirstChild("Attack 2")
local swingAnimation2 = character.Humanoid:LoadAnimation(animation2)

local canSwing = true

local debounce = 0.9

local doneAttack1 = false

script.Parent.Activated:Connect(function()
	if canSwing then
		if doneAttack1 == false then
			canSwing = false
			swingAnimation1:Play()
			wait(debounce)
			canSwing = true
		elseif doneAttack1 then
			canSwing = false
			swingAnimation2:Play()
			wait(debounce)
			canSwing = true
		end
	end
end)

You can Probably use a RayCast or Magnitude to detect whether a Player or NPC is close to the Sword

1 Like

You could put an event in the server script for the tool to listen for when the tool is activated and check for the nearest player using magnitude and deal damage to the nearest player if that’s what you are looking for. As it does sound like you want some type of “ability” for when you activate the tool versus when the tool actually hits something.

1 Like