local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local RaycastHitbox = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("RaycastHitboxV4"))
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local animator = Humanoid:WaitForChild("Animator")
local Tool = script.Parent
local Motor6D = Tool:FindFirstChild("Motor6D")

local mouseDown = false
local mouse = player:GetMouse()

local Character = script.Parent.Parent
local Hitbox = RaycastHitbox.new(Character)

local canSwing = true

local variables = {
	damage = 30,
	hitboxTime = 0.5,
	timeBetweenSwings = 1.4
}

if not Motor6D then
	warn("Motor6D not found in Tool")
	return
end

local function loadAnimation(animationName)
	local animationFolder = ReplicatedStorage:FindFirstChild("Animations")
	if not animationFolder then
		warn("Animations folder not found in ReplicatedStorage")
		return nil
	end

	local toolAnimations = animationFolder:FindFirstChild(Tool.Name)
	if not toolAnimations then
		warn("Tool animations not found in Animations folder")
		return nil
	end

	local animation = toolAnimations:FindFirstChild(animationName)
	if not animation then
		warn("Animation '" .. animationName .. "' not found for tool " .. Tool.Name)
		return nil
	end

	return animator:LoadAnimation(animation)
end

local animations = {
	equip = loadAnimation("Equip"),
	idle = loadAnimation("Idle"),
	slash1 = loadAnimation("Slash1"),
	slash2 = loadAnimation("Slash2"),
	slash3 = loadAnimation("Slash3")
}

local isEquipped = false
local slashIndex = 1
local slashAnimations = {"slash1", "slash2", "slash3"}

Tool.Equipped:Connect(function()
	if not isEquipped then
		isEquipped = true
	else
		warn("isEquipped bool was true before equipped!")
	end

	local rightArm = character:FindFirstChild("Right Arm")
	if not rightArm then
		warn("Right Arm not found in character")
		return
	end

	local rightGrip = rightArm:FindFirstChild("RightGrip")
	if rightGrip then
		rightGrip:Destroy()
	end

	Motor6D.Part0 = rightArm

	if animations.equip then
		animations.equip:Play()
	end
	if animations.idle then
		animations.idle:Play()
	end
end)

Tool.Unequipped:Connect(function()
	isEquipped = false
	for _, animation in pairs(animations) do
		if animation and animation.IsPlaying then
			animation:Stop()
			Hitbox:HitStop()
		end
	end
end)



local function swing()
	if isEquipped and canSwing then
		canSwing = false
		for name, animation in pairs(animations) do
			if name ~= "idle" and animation and animation.IsPlaying then
				animation:Stop()
			end
		end

		local currentSlash = animations[slashAnimations[slashIndex]]
		if currentSlash then
			currentSlash:Play()
			task.spawn(function()
				Hitbox:HitStart()
				task.wait(variables.hitboxTime)
				Hitbox:HitStop()
			end)
			task.wait(variables.timeBetweenSwings)
		else
			warn("Slash animation not found: " .. slashAnimations[slashIndex])
		end

		slashIndex = (slashIndex % #slashAnimations) + 1
		canSwing = true
	end
end

mouse.Button1Down:Connect(function()
	mouseDown = true
	task.spawn(function()
		while mouseDown do
			swing()
			task.wait()
		end
	end)
end)

mouse.Button1Up:Connect(function()
	mouseDown = false
end)



Hitbox.OnHit:Connect(function(hit, humanoid)
	if humanoid ~= Humanoid then
		humanoid:TakeDamage(variables.damage)
	end
end)

is there a point of using the task.spawn() in the button1Down function?