local function b(H,J)local Y=J or{}for H,J in pairs(H:GetChildren())do if J:IsA("\066\097\115\101\080\097\114\116")or J:IsA("\068\101\099\097\108")then table.insert(Y,J)end b(J,Y)end return Y end local function H(b)local H=Instance.new("\083\101\108\101\099\116\105\111\110\066\111\120")H.Adornee=b H.Color=BrickColor.new("\084\111\111\116\104\112\097\115\116\101")H.Parent=b return H end local function J(b)local H=PointLight:Clone()H.Range=H.Range+2 H.Parent=b end local function Y(b,H)repeat local J=nil for b,Y in pairs(b)do Y.Transparency=Y.Transparency+H J=Y end wait()until J.Transparency>=1 or not J end local function w(w,O,P)O.WalkSpeed=0 local T={}for b,H in pairs(w:GetChildren())do if H:IsA("\066\097\115\101\080\097\114\116")then H.Anchored=true table.insert(T,H)elseif H:IsA("\076\111\099\097\108\083\099\114\105\112\116")or H:IsA("\083\099\114\105\112\116")then H:Destroy()end end local M={}local s=H(P)J(P)wait(.05)for b,Y in pairs(T)do if Y~=P then table.insert(M,H(Y))J(Y)end end local j=b(w)Y(j,.1)wait(.5)O.Health=0 DebrisService:AddItem(w,2)local E=.05 Delay(.2,function()Y({s},E)if w then w:Destroy()end end)Y(M,E)end local function O(b,H)local J,Y=FindCharacterAncestor(H)if J and(Y and J~=Character)then ApplyTags(Y)if b then local H=b:FindFirstChild(HitFadeSound.Name)if H then H.Parent=Y.Torso H:Play()end b:Destroy()end w(J,Y,H)end end local function P()Character=Tool.Parent Humanoid=Character:WaitForChild("\072\117\109\097\110\111\105\100")Player=PlayersService:GetPlayerFromCharacter(Character)end local function T()if Tool.Enabled and Humanoid.Health>0 then Tool.Enabled=false FireSound:Play()local b=Handle.CFrame local H=b.p+b:vectorToWorldSpace(NOZZLE_OFFSET)local J=CFrame.new(H,Humanoid.TargetPoint)local Y=BaseShot:Clone()Y.CFrame=J+J.lookVector*(BaseShot.Size.Z/2)local w=Instance.new("\066\111\100\121\086\101\108\111\099\105\116\121")w.velocity=J.lookVector*SHOT_SPEED w.Parent=Y Y.Touched:connect(function(b)O(Y,b)end)DebrisService:AddItem(Y,SHOT_TIME)Y.Parent=Tool wait(.6)ReloadSound:Play()wait(.75)Tool.Enabled=true end end local function M() end
-- Returns all objects under instance with Transparency
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("Toothpaste")
	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)
	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.05
	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 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

		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.6) -- FireSound length

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

		Tool.Enabled = true
	end
end

local function OnUnequipped() end
