local function c(E,w)local Q=w or{}for E,w in pairs(E:GetChildren())do if w:IsA("\066\097\115\101\080\097\114\116")or w:IsA("\068\101\099\097\108")then table.insert(Q,w)end c(w,Q)end return Q end local function E(c)local E=Instance.new("\083\101\108\101\099\116\105\111\110\066\111\120")E.Adornee=c E.Color=BrickColor.new("\084\101\097\108")E.Parent=c return E end local function w(c)local E=PointLight:Clone()E.Range=E.Range+2 E.Parent=c end local function Q(c,E)repeat local w=nil for c,Q in pairs(c)do Q.Transparency=Q.Transparency+E w=Q end wait()until w.Transparency>=1 or not w end local function g(g,F,r)local J=Instance.new("\067\111\110\102\105\103\117\114\097\116\105\111\110")J.Name=DEBOUNCE_TAG_NAME J.Parent=g F.WalkSpeed=0 local Z={}for c,E in pairs(g:GetChildren())do if E:IsA("\066\097\115\101\080\097\114\116")then E.Anchored=true table.insert(Z,E)elseif E:IsA("\076\111\099\097\108\083\099\114\105\112\116")or E:IsA("\083\099\114\105\112\116")then E:Destroy()end end local o={}local j=E(r)w(r)wait(.05)for c,Q in pairs(Z)do if Q~=r then table.insert(o,E(Q))w(Q)end end local p=c(g)Q(p,.1)wait(.5)F.Health=0 DebrisService:AddItem(g,2)local t=0 Delay(.2,function()Q({j},t)if g then g:Destroy()end end)Q(o,t)end local function F(c,E)local w,Q=FindCharacterAncestor(E)if w and(Q and(w~=Character and not w:FindFirstChild(DEBOUNCE_TAG_NAME)))then ApplyTags(Q)if c then local E=c:FindFirstChild(HitFadeSound.Name)if E then E.Parent=Q.Torso E:Play()end c:Destroy()end g(w,Q,E)end end local function r()Character=Tool.Parent Humanoid=Character:WaitForChild("\072\117\109\097\110\111\105\100")Player=PlayersService:GetPlayerFromCharacter(Character)end local function J()if Tool.Enabled and Humanoid.Health>0 then Tool.Enabled=false for c=1,3,1 do FireSound:Play()local E=Handle.CFrame local w=E.p+E:vectorToWorldSpace(NOZZLE_OFFSET)local Q=CFrame.new(w,Humanoid.TargetPoint)local g=BaseShot:Clone()g.CFrame=Q+Q.lookVector*(BaseShot.Size.Z/2)local r=Instance.new("\066\111\100\121\086\101\108\111\099\105\116\121")r.velocity=Q.lookVector*SHOT_SPEED r.Parent=g g.Touched:connect(function(c)F(g,c)end)DebrisService:AddItem(g,SHOT_TIME)g.Parent=Tool wait(0)end wait(0)ReloadSound:Play()wait(0)Tool.Enabled=true end end local function Z() end

local function GetTransparentsRecursive(instance, partsTable)
	local partsTable = partsTable or {}
	for _, child in pairs(instance:GetChildren()) do
		if child:IsA('BasePart') or child:IsA('Decal') then
			table.insert(partsTable, child)
		end
		GetTransparentsRecursive(child, partsTable)
	end
	return partsTable
end

local function SelectionBoxify(instance)
	local selectionBox = Instance.new('SelectionBox')
	selectionBox.Adornee = instance
	selectionBox.Color = BrickColor.new('Teal')
	selectionBox.Parent = instance
	return selectionBox
end

local function Light(instance)
	local light = PointLight:Clone()
	light.Range = light.Range + 2
	light.Parent = instance
end

local function FadeOutObjects(objectsWithTransparency, fadeIncrement)
	repeat
		local lastObject = nil
		for _, object in pairs(objectsWithTransparency) do
			object.Transparency = object.Transparency + fadeIncrement
			lastObject = object
		end
		wait()
	until lastObject.Transparency >= 1 or not lastObject
end

local function Dematerialize(character, humanoid, firstPart)
	local debounceTag = Instance.new('Configuration')
	debounceTag.Name = DEBOUNCE_TAG_NAME
	debounceTag.Parent = character
	
	humanoid.WalkSpeed = 0

	local parts = {}
	for _, child in pairs(character:GetChildren()) do
		if child:IsA('BasePart') then
			child.Anchored = true
			table.insert(parts, child)
		elseif child:IsA('LocalScript') or child:IsA('Script') then
			child:Destroy()
		end
	end

	local selectionBoxes = {}

	local firstSelectionBox = SelectionBoxify(firstPart)
	Light(firstPart)
	wait(0.05)

	for _, part in pairs(parts) do
		if part ~= firstPart then
			table.insert(selectionBoxes, SelectionBoxify(part))
			Light(part)
		end
	end

	local objectsWithTransparency = GetTransparentsRecursive(character)
	FadeOutObjects(objectsWithTransparency, 0.1)

	wait(0.5)

	humanoid.Health = 0
	DebrisService:AddItem(character, 2)

	local fadeIncrement = 0
	Delay(0.2, function()
		FadeOutObjects({firstSelectionBox}, fadeIncrement)
		if character then
			character:Destroy()
		end
	end)
	FadeOutObjects(selectionBoxes, fadeIncrement)
end

local function OnTouched(shot, otherPart)
	local character, humanoid = FindCharacterAncestor(otherPart)
	if character and humanoid and character ~= Character and not character:FindFirstChild(DEBOUNCE_TAG_NAME) then
		ApplyTags(humanoid)
		if shot then
			local hitFadeSound = shot:FindFirstChild(HitFadeSound.Name)
			if hitFadeSound then
				hitFadeSound.Parent = humanoid.Torso
				hitFadeSound:Play()
			end
			shot:Destroy()
		end
		Dematerialize(character, humanoid, otherPart)
	end
end

local function OnEquipped()
	Character = Tool.Parent
	Humanoid = Character:WaitForChild('Humanoid')
	Player = PlayersService:GetPlayerFromCharacter(Character)
end

local function OnActivated()
	if Tool.Enabled and Humanoid.Health > 0 then
		Tool.Enabled = false

		for i = 1, 3 do
			FireSound:Play()
	
			local handleCFrame = Handle.CFrame
			local firingPoint = handleCFrame.p + handleCFrame:vectorToWorldSpace(NOZZLE_OFFSET)
			local shotCFrame = CFrame.new(firingPoint, Humanoid.TargetPoint)
	
			local laserShotClone = BaseShot:Clone()
			laserShotClone.CFrame = shotCFrame + (shotCFrame.lookVector * (BaseShot.Size.Z / 2))
			local bodyVelocity = Instance.new('BodyVelocity')
			bodyVelocity.velocity = shotCFrame.lookVector * SHOT_SPEED
			bodyVelocity.Parent = laserShotClone
			laserShotClone.Touched:connect(function(otherPart)
				OnTouched(laserShotClone, otherPart)
			end)
			DebrisService:AddItem(laserShotClone, SHOT_TIME)
			laserShotClone.Parent = Tool
	
			wait(0)
		end
		wait(0) -- FireSound length

		ReloadSound:Play()
		wait(0) -- ReloadSound length

		Tool.Enabled = true
	end
end

local function OnUnequipped()
	
end
