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