--Made by Luckymaxer

Tool = script.Parent
Handle = Tool:WaitForChild("Handle")
RunAnim = Tool:WaitForChild("RunAnim")
Returned = Tool:WaitForChild("Returned")
ProjectileScript = script:WaitForChild("ProjectileScript")

StandardC1 = CFrame.new(-0.5, 0.5, 0) * CFrame.Angles(0, (math.pi / 2), 0)

RunAnim.Value = ""

OriginalGrip = Tool.Grip
CurrentGrip = OriginalGrip

Debris = game:GetService("Debris")
Players = game:GetService("Players")

function SwordUp()
	CurrentGrip = OriginalGrip
end

function SwordOut()
	CurrentGrip = OriginalGrip * CFrame.Angles((math.pi / 4), 0.4, 0)
end

function SpinSword(SpinTime)
	Spawn(function()
		local StartSpin = tick()
		local EndSpin = StartSpin + SpinTime
		while tick() < EndSpin do
			Tool.Grip = CurrentGrip * CFrame.Angles(math.pi * 2 * ((tick() - StartSpin) / SpinTime), 0, 0)
			wait()
		end
		Tool.Grip = CurrentGrip
	end)
end

Tool.Equipped:connect(function(Mouse)
	Equipped = true
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChild("Humanoid")
	Torso = Character:FindFirstChild("Torso")
	RightShoulder = Torso:FindFirstChild("Right Shoulder")
	local CurrentLast = LastEquipped
	Mouse.KeyDown:connect(function(Key)
		local Key = Key:lower()
		if Key == "q" and Tool.Enabled then
			if Player and Character and Humanoid and Torso and RightShoulder and Returned.Value then
				Tool.Enabled = false
				Returned.Value = false
				for i = 1, 15 do
					RightShoulder.C1 = StandardC1 * CFrame.Angles(0, 0, (-i * (math.pi / 30)))
					wait()
				end
				wait(0.1)
				TargetPoint = Humanoid.TargetPoint
				TargetDir = Torso.CFrame:pointToObjectSpace(TargetPoint) - Vector3.new(1, 0.5, 0)
				TargetVertAngle = math.atan(TargetDir.Y / TargetDir.Z)
				if TargetDir.Z > 0 then
					if TargetDir.X > 0 then 
						TargetHorAngle = (math.pi / 2)
					else
						TargetHorAngle = (-math.pi / 2)
					end
				else
					TargetHorAngle = math.atan(TargetDir.X / TargetDir.magnitude)
				end
				for i = 1, 5 do
					wait()
					RightShoulder.C1 = StandardC1 * CFrame.Angles((i * (TargetHorAngle / 5)), 0, (((i - 5) * (math.pi / 10)) + (i * (TargetVertAngle / 5))))
				end
				ProjectileScriptClone = ProjectileScript:Clone()
				ProjectileScriptClone.TargetPos.Value = Humanoid.TargetPoint
				ProjectileScriptClone.Disabled = false
				ProjectileScriptClone.Parent = Tool
				wait(0.2)
				for i = 10, 0, -1 do
					wait()
					RightShoulder.C1 = StandardC1 * CFrame.Angles((i * (TargetHorAngle / 10)), 0, 0)
				end
			else
				Tool.Enabled = true
			end
		end
	end)
	if not Tool.Enabled then
		wait(5)
		Tool.Enabled = true
	end
end)

Tool.Unequipped:connect(function()
	Equipped = false
	Handle.Transparency = 0.0
	if RightShoulder then
		RightShoulder.C1 = StandardC1
	end
end)

RunAnim.Changed:connect(function()
	local Anim = Tool:FindFirstChild(RunAnim.Value)
	if Anim and Humanoid then
		TheAnim = Humanoid:LoadAnimation(Anim)
		if TheAnim and Humanoid.Health > 0 then
			TheAnim:Play()
			if RunAnim.Value == "RightSlash" or RunAnim.Value == "LeftSlash" or RunAnim.Value == "OverHeadSwing" then
				SpinSword(0.5)
			end
			if RunAnim.Value == "OverHeadSwing" then
				wait(0.25)
				SwordOut()
				wait(0.5)
				SwordUp()
				Tool.Grip = CurrentGrip
			elseif RunAnim.Value == "OverHeadSwingFast" then
				wait(0.125)
				SwordOut()
				wait(0.25)
				SwordUp()
				Tool.Grip = CurrentGrip
			end
		end
	end
end)