BLOXBUILDS
Discord
Back
COMBAT

Sword Tool

Paste as a Script inside a Tool that has a Handle. Click to swing. A hit deals damage once per swing, then the sword waits out its cooldown. Hits tag the attacker for Kill / Death Counter.

-- Paste as a Script inside a Tool that contains a part named Handle.

local Players = game:GetService("Players")

local DAMAGE = 20
local COOLDOWN = 0.6

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local swinging = false
local hitThisSwing: { [Humanoid]: boolean } = {}

local function markKiller(humanoid: Humanoid, player: Player)
	local tag = humanoid:FindFirstChild("creator")
	if not tag then
		tag = Instance.new("ObjectValue")
		tag.Name = "creator"
		tag.Parent = humanoid
	end
	if tag:IsA("ObjectValue") then
		tag.Value = player
	end
end

tool.Activated:Connect(function()
	if swinging then
		return
	end
	swinging = true
	hitThisSwing = {}
	task.delay(COOLDOWN, function()
		swinging = false
	end)
end)

handle.Touched:Connect(function(hit)
	if not swinging then
		return
	end
	local character = tool.Parent
	local attacker = character and character:FindFirstChildOfClass("Humanoid")
	local humanoid = hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid == attacker or hitThisSwing[humanoid] or humanoid.Health <= 0 then
		return
	end

	hitThisSwing[humanoid] = true
	local player = Players:GetPlayerFromCharacter(character)
	if player then
		markKiller(humanoid, player)
	end
	humanoid:TakeDamage(DAMAGE)
end)