function IsTeamMate(B,H)return B and(H and(not B.Neutral and(not H.Neutral and B.TeamColor==H.TeamColor)))end function TagHumanoid(B,H)local t=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")t.Name="\099\114\101\097\116\111\114"t.Value=H Debris:AddItem(t,2)t.Parent=B end function UntagHumanoid(B)for B,H in pairs(GetChildren(B))do if IsA(H,"\079\098\106\101\099\116\086\097\108\117\101")and H.Name=="\099\114\101\097\116\111\114"then Destroy(H)end end end local B,H,t local E=Random.new(tick())local g={}function Touched(H)if not H or not H.Parent then return end g[1]=H local W=NewRay(Handle.Position,((H.Position-Handle.Position).Unit*math.max(Handle.Size.X,Handle.Size.Y,Handle.Size.Z))*2)local d=workspace:FindPartOnRayWithWhitelist(W,g,true)if d~=H then return end local U,D=FindFirstChildOfClass(H.Parent,"\070\111\114\099\101\070\105\101\108\100"),FindFirstChildOfClass(H.Parent,"\072\117\109\097\110\111\105\100")if U or not D or D==t or D.Health<=0 then return end if IsTeamMate(B,Players:GetPlayerFromCharacter(D.Parent))then return end if Properties.Special then Properties.Special=false pcall(function()Handle.Sparkle_Large_Blue.Rate=Handle.Sparkle_Large_Blue.Rate*8 end)local H=Clone(WaitForChild(script,"\067\111\110\115\116\101\108\108\097\116\105\111\110\095\076\105\110\107"))local t=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")t.Name="\072\097\110\100\108\101\082\101\102"t.Value=Handle t.Parent=H t=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")t.Name="\067\114\101\097\116\111\114"t.Value=B t.Parent=H t=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")t.Name="\073\110\105\116\105\097\108\084\097\114\103\101\116"t.Value=D t.Parent=H H.Parent=ServerScriptService H.Disabled=false H.AncestryChanged:Connect(function(B,t)if t then return end if B~=H then return end print("\082\101\108\111\097\100\105\110\103\046\046\046")pcall(function()Handle.Sparkle_Large_Blue.Enabled=false Handle.Sparkle_Large_Blue.Rate=Handle.Sparkle_Large_Blue.Rate/8 end)delay(Properties.Special_Reload,function()pcall(function()Handle.Sparkle_Large_Blue.Enabled=true end)Properties.Special=true end)end)end UntagHumanoid(D)TagHumanoid(D,B)local z=E:NextNumber(Properties.DamageValues[Properties.DamageMode].Min,Properties.DamageValues[Properties.DamageMode].Max)D:TakeDamage(z)if D.Health<=0 then local B=Clone(WaitForChild(script,"\070\105\110\105\115\104\101\114"))B.Parent=D.Parent B.Disabled=false end end local W=tick()function Activated()if not Tool.Enabled then return end Tool.Enabled=false local B=tick()if B-W<=.2 then for B,H in pairs(GetChildren(Handle))do if H and IsA(H,"\080\097\114\116\105\099\108\101\069\109\105\116\116\101\114")then(coroutine.wrap(function()local B=0 repeat local t=RunService.Heartbeat:Wait()B=B+t H:Emit(H.Enabled and math.ceil(H.Rate*.25)or 0)until B>=.5 end))()end end Sounds.Lunge:Play()Tool.Grip=DefaultGrip.Value*CFrame.Angles(math.rad(90),0,0)local B=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")B.Name="\116\111\111\108\097\110\105\109"B.Value="\076\117\110\103\101"B.Parent=Tool Properties.DamageMode="\076\117\110\103\101"wait(.5)Properties.DamageMode="\078\111\114\109\097\108"Tool.Grip=DefaultGrip.Value wait(.5)else Sounds.Slash.TimePosition=Sounds.Slash.TimeLength/2 Sounds.Slash:Play()local B=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")B.Name="\116\111\111\108\097\110\105\109"B.Value="\083\108\097\115\104"B.Parent=Tool end W=B Tool.Enabled=true end local d={}function Equipped()H=Tool.Parent if not H then return end t=FindFirstChildOfClass(H,"\072\117\109\097\110\111\105\100")if not t or t.Health<=0 then return end Sounds.Equip:Play()B=Players:GetPlayerFromCharacter(H)if B then d[#d+1]=B.CharacterRemoving:Connect(Unequipped)end d[#d+1]=Handle.Touched:Connect(Touched)d[#d+1]=Tool.Activated:Connect(Activated)d[#d+1]=t.Died:Connect(Unequipped)t.WalkSpeed=Properties.SpeedCap d[#d+1]=(t:GetPropertyChangedSignal("\087\097\108\107\083\112\101\101\100")):Connect(function()if not t then return end t.WalkSpeed=Properties.SpeedCap end)end function Unequipped()for B=1,#d,1 do if d[B]then d[B]:Disconnect()end end d={}if t then t.WalkSpeed=16 end end Tool.Equipped:Connect(Equipped)Tool.Unequipped:Connect(Unequipped)


function IsTeamMate(Player1, Player2)
	return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
	for i, v in pairs(GetChildren(humanoid)) do
		if IsA(v,"ObjectValue") and v.Name == "creator" then
			Destroy(v)
		end
	end
end

local Player, Character, Humanoid

local Seed = Random.new(tick())

local Hit_WhiteList = {}
function Touched(hit)
	
	if not hit or not hit.Parent then return end
	
	Hit_WhiteList[1] = hit --Replace the Whitelisted entry (Also helps reduce lag)
	
	--Do a Server-sided check to make sure the sword is in reasonable distance to the part hit (To prevent exploits)
	
	local DistanceRay = NewRay( Handle.Position , (hit.Position - Handle.Position).Unit * math.max(Handle.Size.X,Handle.Size.Y,Handle.Size.Z) * 2 )
	
	local part = workspace:FindPartOnRayWithWhitelist(DistanceRay, Hit_WhiteList , true)	
	
	--warn((part and "Part in range") or "Part not in range")
	--assert(part, "Part not in range: Potential exploit")
	if part ~= hit then return end -- Potential exploiter may be abusing the touched events
	
	local FF, Hum = FindFirstChildOfClass(hit.Parent, "ForceField"), FindFirstChildOfClass(hit.Parent, "Humanoid")
	
	if FF or not Hum or Hum == Humanoid or Hum.Health <= 0 then return end --Don't hit yourself or anyone with a ForceField now..
	
	if IsTeamMate(Player, Players:GetPlayerFromCharacter(Hum.Parent)) then return end
	
	if Properties.Special then
		Properties.Special = false
		
		pcall(function()
			--Handle["Sparkle_Large_Blue"].Enabled = false
			Handle["Sparkle_Large_Blue"].Rate = Handle["Sparkle_Large_Blue"].Rate * 8
		end)
		
		local Constellation_Link = Clone(WaitForChild(script, "Constellation_Link"))
		
		local Val = Instance.new("ObjectValue")
		Val.Name = "HandleRef"
		Val.Value = Handle
		Val.Parent = Constellation_Link
		
		Val = Instance.new("ObjectValue")
		Val.Name = "Creator"
		Val.Value = Player
		Val.Parent = Constellation_Link
		
		Val = Instance.new("ObjectValue")
		Val.Name = "InitialTarget"
		Val.Value = Hum
		Val.Parent = Constellation_Link
		
		Constellation_Link.Parent = ServerScriptService
		
		Constellation_Link.Disabled = false
		
		Constellation_Link.AncestryChanged:Connect(function(obj, parent)
			
			if parent then return end
			
			if obj ~= Constellation_Link then return end
			
			print("Reloading...")
			pcall(function()
				Handle["Sparkle_Large_Blue"].Enabled = false
				Handle["Sparkle_Large_Blue"].Rate = Handle["Sparkle_Large_Blue"].Rate / 8
			end)
			delay(Properties.Special_Reload, function()
				pcall(function()
					Handle["Sparkle_Large_Blue"].Enabled = true
				end)
				Properties.Special = true
			end)
			
		end)
	end
	
	UntagHumanoid(Hum)
	TagHumanoid(Hum, Player)
	
	local Damage = Seed:NextNumber(Properties["DamageValues"][Properties.DamageMode].Min, Properties["DamageValues"][Properties.DamageMode].Max)
	
	Hum:TakeDamage(Damage)
	
	if Hum.Health <= 0 then
		--Finisher activated!
		local Finisher = Clone(WaitForChild(script, "Finisher"))
		Finisher.Parent = Hum.Parent
		Finisher.Disabled = false
	end
	
end

local LastAttack = tick()
function Activated()
	
	if not Tool.Enabled then return end
	
	Tool.Enabled = false
	
	local CurrentTime = tick()
	
	if (CurrentTime - LastAttack) <= 0.2 then
		
		for _,v in pairs(GetChildren(Handle)) do
			if v and IsA(v, "ParticleEmitter") then
				coroutine.wrap(function()
					local x = 0
					repeat
						local delta = RunService.Heartbeat:Wait()
						x = x + delta
						v:Emit((v.Enabled and math.ceil(v.Rate * .25)) or 0)
					until x >= .5
				end)()
				
			end
		end
		
		Sounds.Lunge:Play()
		
		Tool.Grip = DefaultGrip.Value * CFrame.Angles(math.rad(90),0,0)
		
		local LungeAnim = Instance.new("StringValue")
		LungeAnim.Name = "toolanim"
		LungeAnim.Value = "Lunge"
		LungeAnim.Parent = Tool
		
		--Lunge
		Properties.DamageMode = "Lunge" --Should allow the sword to deal more damage while lunging
		--print("Lunge")
		wait(.5)
		Properties.DamageMode = "Normal"
		Tool.Grip = DefaultGrip.Value
		wait(.5)
	else
		--Slash
		Sounds.Slash.TimePosition = Sounds.Slash.TimeLength/2
		Sounds.Slash:Play()
		local SwingAnim = Instance.new("StringValue")
		SwingAnim.Name = "toolanim"
		SwingAnim.Value = "Slash"
		SwingAnim.Parent = Tool
		--print("Swing")
	end
	
	LastAttack = CurrentTime
	
	Tool.Enabled = true
	
end


local Events = {} 

function Equipped()
	
	Character = Tool.Parent
	
	if not Character then return end
	
	Humanoid = FindFirstChildOfClass(Character, "Humanoid")
	
	if not Humanoid or Humanoid.Health <= 0 then return end
	
	Sounds.Equip:Play()
	
	Player = Players:GetPlayerFromCharacter(Character)
	
	if Player then
		Events[#Events+1] = Player.CharacterRemoving:Connect(Unequipped) --To keep some things from breaking
	end
	
	Events[#Events+1] = Handle.Touched:Connect(Touched)
	
	Events[#Events+1] = Tool.Activated:Connect(Activated)
	
	Events[#Events+1] = Humanoid.Died:Connect(Unequipped)
	
	Humanoid.WalkSpeed = Properties.SpeedCap
	Events[#Events+1] = Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
		if not Humanoid then return end
		Humanoid.WalkSpeed = Properties.SpeedCap
	end)
	
	
	
end


function Unequipped()
	
	for i=1,#Events,1 do
		if Events[i] then
			Events[i]:Disconnect()
		end
	end
	Events = {}
	
	if Humanoid then
		Humanoid.WalkSpeed = 16
	end
end

Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(Unequipped)

