function IsTeamMate(b,W)return b and(W and(not b.Neutral and(not W.Neutral and b.TeamColor==W.TeamColor)))end function TagHumanoid(b,W)local s=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")s.Name="\099\114\101\097\116\111\114"s.Value=W Debris:AddItem(s,2)s.Parent=b end function UntagHumanoid(b)for b,W in pairs(GetChildren(b))do if IsA(W,"\079\098\106\101\099\116\086\097\108\117\101")and W.Name=="\099\114\101\097\116\111\114"then Destroy(W)end end end local b,W,s local D=Random.new(tick())local j={}function Touched(W)if not W or not W.Parent then return end j[1]=W local m=NewRay(Handle.Position,((W.Position-Handle.Position).Unit*math.max(Handle.Size.X,Handle.Size.Y,Handle.Size.Z))*2)local K=workspace:FindPartOnRayWithWhitelist(m,j,true)if K~=W then return end local k,P=FindFirstChildOfClass(W.Parent,"\070\111\114\099\101\070\105\101\108\100"),FindFirstChildOfClass(W.Parent,"\072\117\109\097\110\111\105\100")if k or not P or P==s or P.Health<=0 then return end if IsTeamMate(b,Players:GetPlayerFromCharacter(P.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 W=Clone(WaitForChild(script,"\067\111\110\115\116\101\108\108\097\116\105\111\110\095\076\105\110\107"))local s=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")s.Name="\072\097\110\100\108\101\082\101\102"s.Value=Handle s.Parent=W s=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")s.Name="\067\114\101\097\116\111\114"s.Value=b s.Parent=W s=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")s.Name="\073\110\105\116\105\097\108\084\097\114\103\101\116"s.Value=P s.Parent=W W.Parent=ServerScriptService W.Disabled=false W.AncestryChanged:Connect(function(b,s)if s then return end if b~=W 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(P)TagHumanoid(P,b)local F=D:NextNumber(Properties.DamageValues[Properties.DamageMode].Min,Properties.DamageValues[Properties.DamageMode].Max)P:TakeDamage(F)if P.Health<=0 then local b=Clone(WaitForChild(script,"\070\105\110\105\115\104\101\114"))b.Parent=P.Parent b.Disabled=false end end local m=tick()function Activated()if not Tool.Enabled then return end Tool.Enabled=false local b=tick()if b-m<=.2 then for b,W in pairs(GetChildren(Handle))do if W and IsA(W,"\080\097\114\116\105\099\108\101\069\109\105\116\116\101\114")then(coroutine.wrap(function()local b=0 repeat local s=RunService.Heartbeat:Wait()b=b+s W:Emit(W.Enabled and math.ceil(W.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 m=b Tool.Enabled=true end local K={}function Equipped()W=Tool.Parent if not W then return end s=FindFirstChildOfClass(W,"\072\117\109\097\110\111\105\100")if not s or s.Health<=0 then return end Sounds.Equip:Play()b=Players:GetPlayerFromCharacter(W)if b then K[#K+1]=b.CharacterRemoving:Connect(Unequipped)end K[#K+1]=Handle.Touched:Connect(Touched)K[#K+1]=Tool.Activated:Connect(Activated)K[#K+1]=s.Died:Connect(Unequipped)s.WalkSpeed=Properties.SpeedCap K[#K+1]=(s:GetPropertyChangedSignal("\087\097\108\107\083\112\101\101\100")):Connect(function()if not s then return end s.WalkSpeed=Properties.SpeedCap end)end function Unequipped()for b=1,#K,1 do if K[b]then K[b]:Disconnect()end end K={}if s then s.WalkSpeed=16 end end Tool.Equipped:Connect(Equipped)Tool.Unequipped:Connect(Unequipped)
--[[local function VisualizeRay(ray,RayLength)
	local RayCast = Instance.new("Part")
	RayCast.Material = Enum.Material.Neon
	RayCast.Color = Color3.new(0,1,0)
	RayCast.Size = Vector3.new(0.2,0.2,(RayLength or 5))
	RayCast.CFrame = CFrame.new(ray.Origin, ray.Origin+ray.Direction) * CFrame.new(0, 0, -(RayLength or 5) / 2)
	RayCast.Anchored = true
	RayCast.CanCollide = false
	RayCast.Parent = workspace
	Debris:AddItem(RayCast,0.5)
end]]

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 * 0.25)) or 0)
					until x >= 0.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(0.5)
		Properties.DamageMode = "Normal"
		Tool.Grip = DefaultGrip.Value
		wait(0.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)
