local G={Duration=0,MaxDuration=5,LinkDamage=10}local z,q,u=script.FindFirstChild,script.FindFirstChildOfClass,script.WaitForChild local t,P=script.Clone,script.Destroy local c=u(script,"\072\097\110\100\108\101\082\101\102",5)local S=u(script,"\067\114\101\097\116\111\114",5)local l=u(script,"\073\110\105\116\105\097\108\084\097\114\103\101\116",5)if not l or not l.Value or not c or not c.Value or not S then script:Destroy()end c=c.Value S=S.Value l=l.Value local H=z(l.Parent,"\084\111\114\115\111")or z(l.Parent,"\085\112\112\101\114\084\111\114\115\111")local Z=q(S.Character,"\072\117\109\097\110\111\105\100")local V=game:FindService("\080\108\097\121\101\114\115")or game:GetService("\080\108\097\121\101\114\115")local M=game:FindService("\068\101\098\114\105\115")or game:GetService("\068\101\098\114\105\115")local A=game:FindService("\082\117\110\083\101\114\118\105\099\101")or game:GetService("\082\117\110\083\101\114\118\105\099\101")print("\083\116\097\114\108\105\103\104\116 \076\105\110\107 \097\098\105\108\105\116\121 \097\099\116\105\118\097\116\101\100\033 \084\097\103 \097\115 \109\117\099\104 \112\101\111\112\108\101 \097\115 \121\111\117 \099\097\110 \098\101\102\111\114\101 "..(G.MaxDuration.." \115\101\099\111\110\100\115 \105\115 \117\112\033"))local X=Ray.new local U,s=script.GetChildren,script.GetDescendants local x=script.IsA function IsTeamMate(G,z)return G and(z and(not G.Neutral and(not z.Neutral and G.TeamColor==z.TeamColor)))end function TagHumanoid(G,z)local q=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")q.Name="\099\114\101\097\116\111\114"q.Value=z M:AddItem(q,2)q.Parent=G end function UntagHumanoid(G)for G,z in pairs(U(G))do if x(z,"\079\098\106\101\099\116\086\097\108\117\101")and z.Name=="\099\114\101\097\116\111\114"then P(z)end end end local L,k={},{}local F=Instance.new("\078\117\109\098\101\114\086\097\108\117\101")F.Value=G.Duration F.Name="\068\117\114\097\116\105\111\110"F.Parent=script local m=Instance.new("\065\116\116\097\099\104\109\101\110\116")L[#L+1]=m m.Name="\067\111\110\115\116\101\108\108\097\116\105\111\110\095\076\105\110\107"m.Position=Vector3.new(0,0,0)m.Parent=H local R=ColorSequence.new local T,v=Color3.fromRGB(0,170,255),Color3.new(1,1,1)local f,I,r={},{},{}local i={}pcall(function()local z=t(script.Sparkle_Large_Blue)r[#r+1]=z L[#L+1]=z z.Parent=H z.Enabled=true k[#k+1]=(F:GetPropertyChangedSignal("\086\097\108\117\101")):Connect(function()z.Color=R(T:Lerp(v,G.Duration/G.MaxDuration),v)end)end)pcall(function()local G=t(script.ExplodeSound)i[#i+1]=G G.Parent=m end)local h=m local Y,w={},{}w[l]=l local C=1 f[#f+1]=l I[#I+1]=m function Link(u)if not u or not u.Parent then return end Y[1]=u local P=X(c.Position,((u.Position-c.Position).Unit*math.max(c.Size.X,c.Size.Y,c.Size.Z))*2)local l=workspace:FindPartOnRayWithWhitelist(P,Y,true)if l~=u then return end local H,M,A=q(u.Parent,"\070\111\114\099\101\070\105\101\108\100"),q(u.Parent,"\072\117\109\097\110\111\105\100"),z(u.Parent,"\084\111\114\115\111")or z(u.Parent,"\085\112\112\101\114\084\111\114\115\111")if H or not M or not A or M==Z or M.Health<=0 or w[M]then return end if IsTeamMate(S,V:GetPlayerFromCharacter(M.Parent))then return end w[M]=M f[#f+1]=M m=Instance.new("\065\116\116\097\099\104\109\101\110\116")I[#I+1]=m pcall(function()local G=t(script.ExplodeSound)i[#i+1]=G G.Parent=m end)L[#L+1]=m m.Name="\067\111\110\115\116\101\108\108\097\116\105\111\110\095\076\105\110\107"m.Position=Vector3.new(0,0,0)if h then pcall(function()local z=t(script.Constellation_Beam)L[#L+1]=z z.Attachment0=h z.Attachment1=m z.Enabled=true z.Parent=m k[#k+1]=(F:GetPropertyChangedSignal("\086\097\108\117\101")):Connect(function()z.Color=R(T:Lerp(v,G.Duration/G.MaxDuration))end)end)end pcall(function()local z=t(script.Sparkle_Large_Blue)L[#L+1]=z r[#r+1]=z z.Parent=A z.Enabled=true k[#k+1]=(F:GetPropertyChangedSignal("\086\097\108\117\101")):Connect(function()z.Color=R(T:Lerp(v,G.Duration/G.MaxDuration),v)end)end)m.Parent=A h=m C=C+1 G.Duration=0 end c.Touched:Connect(Link)local K=false while G.Duration<G.MaxDuration and not K do local z=A.Heartbeat:Wait()G.Duration=G.Duration+z F.Value=G.Duration end
--//The logic behind the Starlight Sword's Chain-reaction ability

local Properties = {
	Duration = 0, -- The current time the ability is active
	MaxDuration = 5, -- How long can the ability delay itself before it fully executes.
	LinkDamage = 10, -- The amount of damage multiplied per-person linked.
}

local FindFirstChild, FindFirstChildOfClass, WaitForChild =
	script.FindFirstChild, script.FindFirstChildOfClass, script.WaitForChild

local Clone, Destroy = script.Clone, script.Destroy

local Handle = WaitForChild(script, "HandleRef", 5)
local Creator = WaitForChild(script, "Creator", 5)
local InitialTarget = WaitForChild(script, "InitialTarget", 5)

if not InitialTarget or not InitialTarget.Value or not Handle or not Handle.Value or not Creator then
	script:Destroy()
end

Handle = Handle.Value
Creator = Creator.Value
InitialTarget = InitialTarget.Value

local TargetTorso = (
	FindFirstChild(InitialTarget.Parent, "Torso") or FindFirstChild(InitialTarget.Parent, "UpperTorso")
)

local Humanoid = FindFirstChildOfClass(Creator.Character, "Humanoid")

local Players = (game:FindService("Players") or game:GetService("Players"))
local Debris = (game:FindService("Debris") or game:GetService("Debris"))
local RunService = (game:FindService("RunService") or game:GetService("RunService"))

print(
	"Starlight Link ability activated! Tag as much people as you can before "
		.. Properties.MaxDuration
		.. " seconds is up!"
)

local NewRay = Ray.new

local GetChildren, GetDescendants = script.GetChildren, script.GetDescendants

local IsA = script.IsA

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 Deletables, Events = {}, {} --Put objects in the tables to discard when we're done.

local DurationValue = Instance.new("NumberValue")
DurationValue.Value = Properties.Duration
DurationValue.Name = "Duration"
DurationValue.Parent = script

local Attach = Instance.new("Attachment")
Deletables[#Deletables + 1] = Attach

Attach.Name = "Constellation_Link"
Attach.Position = Vector3.new(0, 0, 0)

Attach.Parent = TargetTorso

local NewColorSequence = ColorSequence.new
local Color_1, Color_2 = Color3.fromRGB(0, 170, 255), Color3.new(1, 1, 1)

local Queue, AttachQueue, SparkleQueue = {}, {}, {}
local Sounds = {}
pcall(function()
	local Sparkles = Clone(script["Sparkle_Large_Blue"])
	SparkleQueue[#SparkleQueue + 1] = Sparkles
	Deletables[#Deletables + 1] = Sparkles

	Sparkles.Parent = TargetTorso

	Sparkles.Enabled = true

	Events[#Events + 1] = DurationValue:GetPropertyChangedSignal("Value"):Connect(function()
		Sparkles.Color = NewColorSequence(Color_1:Lerp(Color_2, Properties.Duration / Properties.MaxDuration), Color_2)
	end)
end)

pcall(function()
	local ExplodeSound = Clone(script.ExplodeSound)
	Sounds[#Sounds + 1] = ExplodeSound
	ExplodeSound.Parent = Attach
end)

local LastAttachment = Attach --Reference the last attachment point we made.

local Hit_WhiteList, TaggedHumanoids = {}, {}
TaggedHumanoids[InitialTarget] = InitialTarget
local Total = 1

Queue[#Queue + 1] = InitialTarget
AttachQueue[#AttachQueue + 1] = Attach

function Link(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, Torso =
		FindFirstChildOfClass(hit.Parent, "ForceField"),
		FindFirstChildOfClass(hit.Parent, "Humanoid"),
		(FindFirstChild(hit.Parent, "Torso") or FindFirstChild(hit.Parent, "UpperTorso"))

	if FF or not Hum or not Torso or Hum == Humanoid or Hum.Health <= 0 or TaggedHumanoids[Hum] then
		return
	end --Don't hit yourself or anyone with a ForceField now..

	if IsTeamMate(Creator, Players:GetPlayerFromCharacter(Hum.Parent)) then
		return
	end

	TaggedHumanoids[Hum] = Hum --We got one!

	Queue[#Queue + 1] = Hum

	Attach = Instance.new("Attachment")
	AttachQueue[#AttachQueue + 1] = Attach

	pcall(function()
		local ExplodeSound = Clone(script.ExplodeSound)
		Sounds[#Sounds + 1] = ExplodeSound
		ExplodeSound.Parent = Attach
	end)

	Deletables[#Deletables + 1] = Attach

	Attach.Name = "Constellation_Link"
	Attach.Position = Vector3.new(0, 0, 0)

	--warn(LastAttachment)

	if LastAttachment then
		--Create a Constellation Beam from one target to another
		pcall(function()
			local Constellation_Beam = Clone(script["Constellation_Beam"])
			Deletables[#Deletables + 1] = Constellation_Beam

			Constellation_Beam.Attachment0 = LastAttachment
			Constellation_Beam.Attachment1 = Attach
			Constellation_Beam.Enabled = true

			Constellation_Beam.Parent = Attach

			Events[#Events + 1] = DurationValue:GetPropertyChangedSignal("Value"):Connect(function()
				Constellation_Beam.Color =
					NewColorSequence(Color_1:Lerp(Color_2, Properties.Duration / Properties.MaxDuration))
			end)

			--print("Linked Targets!")
		end)
	end

	pcall(function()
		local Sparkles = Clone(script["Sparkle_Large_Blue"])
		Deletables[#Deletables + 1] = Sparkles
		SparkleQueue[#SparkleQueue + 1] = Sparkles

		Sparkles.Parent = Torso

		Sparkles.Enabled = true

		Events[#Events + 1] = DurationValue:GetPropertyChangedSignal("Value"):Connect(function()
			Sparkles.Color =
				NewColorSequence(Color_1:Lerp(Color_2, Properties.Duration / Properties.MaxDuration), Color_2)
		end)
	end)

	Attach.Parent = Torso

	LastAttachment = Attach --Reference the last attachment point we made.

	Total = Total + 1

	--warn("Tagged " .. Hum.Parent.Name .. "!", "\n Pending damage: " .. Properties.LinkDamage * Total)

	Properties.Duration = 0 --Reset the duration for every time you've tagged a person.
end

Handle.Touched:Connect(Link)
local Canceled = false

while Properties.Duration < Properties.MaxDuration and not Canceled do
	local delta = RunService.Heartbeat:Wait()
	Properties.Duration = Properties.Duration + delta
	DurationValue.Value = Properties.Duration
	--print(Properties.Duration)
end
