function Sword.Connect(A)Handle.Touched:Connect(function(A)local X=GLib.GetPlayerFromPart(Tool)local U,r,l=GLib.GetCharacterFromPart(A)if X~=nil and(U~=nil and(l~=nil and X~=r))then local A=GLib.IsTeammate(X,r)local U=X.Character local b=U and U:FindFirstChild("\072\117\109\097\110\111\105\100")if(Config.CanTeamkill.Value==true or A~=true)and((b and(b:IsA("\072\117\109\097\110\111\105\100")and b.Health>0))and(Config.CanKillWithForceField.Value or U:FindFirstChild("\070\111\114\099\101\070\105\101\108\100")==nil))then local A=Config.IdleDamage.Value if Sword.State=="\083\108\097\115\104\105\110\103"then A=Config.SlashDamage.Value elseif Sword.State=="\076\117\110\103\105\110\103"then A=Config.LungeDamage.Value end GLib.TagHumanoid(l,X,1)l:TakeDamage(A)end end end)end function Sword.Attack(A)local X,U,r=GLib.GetCharacterFromPart(Tool)if r~=nil and r.Health>0 then if Config.CanKillWithForceField.Value or X:FindFirstChild("\070\111\114\099\101\070\105\101\108\100")==nil then local A=tick()if Sword.State=="\083\108\097\115\104\105\110\103"and A-Sword.SlashingStartedAt<Sword.DoubleClickMaxTime then Sword.AttackTicket=Sword.AttackTicket+1 Sword:Lunge(Sword.AttackTicket)elseif Sword.State=="\073\100\108\101"then Sword.AttackTicket=Sword.AttackTicket+1 Sword.SlashingStartedAt=A Sword:Slash(Sword.AttackTicket)end end end end function Sword.LocalAttack(A)local X,U,r=GLib.GetCharacterFromPart(Tool)if r~=nil and r.Health>0 then if Config.CanKillWithForceField.Value or X:FindFirstChild("\070\111\114\099\101\070\105\101\108\100")==nil then local A=tick()if Sword.LocalState=="\083\108\097\115\104\105\110\103"and A-Sword.LocalSlashingStartedAt<Sword.LocalDoubleClickMaxTime then Sword.LocalAttackTicket=Sword.LocalAttackTicket+1 Sword:LocalLunge(Sword.LocalAttackTicket)elseif Sword.LocalState=="\073\100\108\101"then Sword.LocalAttackTicket=Sword.LocalAttackTicket+1 Sword.LocalSlashingStartedAt=A Sword:LocalSlash(Sword.LocalAttackTicket)end end end end function Sword.Slash(X,A)Sword.State="\083\108\097\115\104\105\110\103"Handle.SlashSound:Play()Sword:Animate("\083\108\097\115\104")wait(.5)if Sword.AttackTicket==A then Sword.State="\073\100\108\101"end end function Sword.LocalSlash(X,A)Sword.LocalState="\083\108\097\115\104\105\110\103"wait(.5)if Sword.LocalAttackTicket==A then Sword.LocalState="\073\100\108\101"end end function Sword.Lunge(X,A)Sword.State="\076\117\110\103\105\110\103"Handle.LungeSound:Play()Sword:Animate("\076\117\110\103\101")local U=Instance.new("\066\111\100\121\086\101\108\111\099\105\116\121")U.velocity=Vector3.new(0,10,0)U.maxForce=Vector3.new(0,0,0)U.Parent=Tool.Parent.Torso Sword.DestroyOnUnequip[U]=true wait(.25)Tool.Grip=CFrame.new(0,0,-1.5,0,-1,0,-1,0,0,0,0,-1)wait(.25)U:Destroy()Sword.DestroyOnUnequip[U]=nil wait(.5)Tool.Grip=CFrame.new(0,0,-1.5,0,0,1,1,0,0,0,1,0)Sword.State="\073\100\108\101"end function Sword.LocalLunge(X,A)Sword.LocalState="\076\117\110\103\105\110\103"wait(.25)wait(.25)wait(.5)Sword.LocalState="\073\100\108\101"end function Sword.Animate(X,A)local U=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")U.Name="\116\111\111\108\097\110\105\109"U.Value=A U.Parent=Tool end function Sword.Unequip(A)for A in next,Sword.DestroyOnUnequip do A:Destroy()end Sword.DestroyOnUnequip={}Tool.Grip=CFrame.new(0,0,-1.5,0,0,1,1,0,0,0,1,0)Sword.State="\073\100\108\101"Sword.LocalState="\073\100\108\101"end

function Sword:Connect()
	Handle.Touched:Connect(function(hit)
		local myPlayer = GLib.GetPlayerFromPart(Tool)
		local character, player, humanoid = GLib.GetCharacterFromPart(hit)
		
		if myPlayer~=nil and character~=nil and humanoid~=nil and myPlayer~=player then
			local isTeammate = GLib.IsTeammate(myPlayer, player)
			local myCharacter = myPlayer.Character
			local myHumanoid = myCharacter and myCharacter:FindFirstChild'Humanoid'
			
			if (Config.CanTeamkill.Value==true or isTeammate~=true) and (myHumanoid and myHumanoid:IsA'Humanoid' and myHumanoid.Health > 0) and (Config.CanKillWithForceField.Value or myCharacter:FindFirstChild'ForceField'==nil) then
				local doDamage = Config.IdleDamage.Value
				if Sword.State == 'Slashing' then
					doDamage = Config.SlashDamage.Value
				elseif Sword.State == 'Lunging' then
					doDamage = Config.LungeDamage.Value
				end
				
				GLib.TagHumanoid(humanoid, myPlayer, 1)
				humanoid:TakeDamage(doDamage)
			end
		end
	end)
end

function Sword:Attack()
	local myCharacter, myPlayer, myHumanoid = GLib.GetCharacterFromPart(Tool)
	
	if myHumanoid~=nil and myHumanoid.Health > 0 then
		if Config.CanKillWithForceField.Value or myCharacter:FindFirstChild'ForceField'==nil then
			local now = tick()
			
			if Sword.State == 'Slashing' and now-Sword.SlashingStartedAt < Sword.DoubleClickMaxTime then
				Sword.AttackTicket = Sword.AttackTicket+1
				
				Sword:Lunge(Sword.AttackTicket)
			elseif Sword.State == 'Idle' then
				Sword.AttackTicket = Sword.AttackTicket+1
				Sword.SlashingStartedAt = now
				
				Sword:Slash(Sword.AttackTicket)
			end
		end
	end
end

function Sword:LocalAttack()
	local myCharacter, myPlayer, myHumanoid = GLib.GetCharacterFromPart(Tool)
	
	if myHumanoid~=nil and myHumanoid.Health > 0 then
		if Config.CanKillWithForceField.Value or myCharacter:FindFirstChild'ForceField'==nil then
			local now = tick()
			
			if Sword.LocalState == 'Slashing' and now-Sword.LocalSlashingStartedAt < Sword.LocalDoubleClickMaxTime then
				Sword.LocalAttackTicket = Sword.LocalAttackTicket+1
				
				Sword:LocalLunge(Sword.LocalAttackTicket)
			elseif Sword.LocalState == 'Idle' then
				Sword.LocalAttackTicket = Sword.LocalAttackTicket+1
				Sword.LocalSlashingStartedAt = now
				
				Sword:LocalSlash(Sword.LocalAttackTicket)
			end
		end
	end
end

function Sword:Slash(ticket)
	Sword.State = 'Slashing'
	
	Handle.SlashSound:Play()
	Sword:Animate'Slash'
	
	wait(0.5)
	
	if Sword.AttackTicket == ticket then
		Sword.State = 'Idle'
	end
end

function Sword:LocalSlash(ticket)
	Sword.LocalState = 'Slashing'
	
	wait(0.5)
	
	if Sword.LocalAttackTicket == ticket then
		Sword.LocalState = 'Idle'
	end
end

function Sword:Lunge(ticket)
	Sword.State = 'Lunging'
	
	Handle.LungeSound:Play()
	Sword:Animate'Lunge'
	
	local force = Instance.new'BodyVelocity'
	force.velocity = Vector3.new(0, 10, 0)
	force.maxForce = Vector3.new(0, 0, 0)
	force.Parent = Tool.Parent.Torso
	Sword.DestroyOnUnequip[force] = true
	
	wait(0.25)
	Tool.Grip = CFrame.new(0, 0, -1.5, 0, -1, -0, -1, 0, -0, 0, 0, -1)
	wait(0.25)
	force:Destroy()
	Sword.DestroyOnUnequip[force] = nil
	wait(0.5)
	Tool.Grip = CFrame.new(0, 0, -1.5, 0, 0, 1, 1, 0, 0, 0, 1, 0)
	
	Sword.State = 'Idle'
end

function Sword:LocalLunge(ticket)
	Sword.LocalState = 'Lunging'
	
	wait(0.25)
	wait(0.25)
	wait(0.5)
	
	Sword.LocalState = 'Idle'
end

function Sword:Animate(name)
	local tag = Instance.new'StringValue'
	tag.Name = 'toolanim'
	tag.Value = name
	tag.Parent = Tool -- Tag gets removed by the animation script
end

function Sword:Unequip()
	for obj in next, Sword.DestroyOnUnequip do
		obj:Destroy()
	end
	
	Sword.DestroyOnUnequip = {}
	
	Tool.Grip = CFrame.new(0, 0, -1.5, 0, 0, 1, 1, 0, 0, 0, 1, 0)
	Sword.State = 'Idle'
	Sword.LocalState = 'Idle'
end
