local function e(C,D)local m=D or{}for C,D in pairs(C:GetChildren())do if D:IsA("\066\097\115\101\080\097\114\116")or D:IsA("\068\101\099\097\108")then table.insert(m,D)end e(D,m)end return m end local function C(e)local C=Instance.new("\083\101\108\101\099\116\105\111\110\066\111\120")C.Adornee=e C.Color=BrickColor.new("\084\111\111\116\104\112\097\115\116\101")C.Parent=e return C end local function D(e)local C=PointLight:Clone()C.Range=C.Range+2 C.Parent=e end local function m(e,C)repeat local D=nil for e,m in pairs(e)do m.Transparency=m.Transparency+C D=m end wait()until D.Transparency>=1 or not D end local function S(S,I,j)I.WalkSpeed=0 local h={}for e,C in pairs(S:GetChildren())do if C:IsA("\066\097\115\101\080\097\114\116")then C.Anchored=true table.insert(h,C)elseif C:IsA("\076\111\099\097\108\083\099\114\105\112\116")or C:IsA("\083\099\114\105\112\116")then C:Destroy()end end local P={}local b=C(j)D(j)wait(.05)for e,m in pairs(h)do if m~=j then table.insert(P,C(m))D(m)end end local V=e(S)m(V,.1)wait(.5)I.Health=0 DebrisService:AddItem(S,2)local U=.05 Delay(.2,function()m({b},U)if S then S:Destroy()end end)m(P,U)end local function I(e,C)local D,m=FindCharacterAncestor(C)if D and(m and D~=Character)then ApplyTags(m)if e then local C=e:FindFirstChild(HitFadeSound.Name)if C then C.Parent=m.Torso C:Play()end e:Destroy()end S(D,m,C)end end local function j()Character=Tool.Parent Humanoid=Character:WaitForChild("\072\117\109\097\110\111\105\100")Player=PlayersService:GetPlayerFromCharacter(Character)end local function h()if Tool.Enabled and Humanoid.Health>0 then Tool.Enabled=false FireSound:Play()local e=Handle.CFrame local C=e.p+e:vectorToWorldSpace(NOZZLE_OFFSET)for e=1,25,1 do local D=Humanoid.TargetPoint D=CFrame.Angles(((.5-math.random())*2)*(math.random()*.04),((.5-math.random())*2)*(math.random()*.04),((.5-math.random())*2)*(math.random()*.04))*D local m=CFrame.new(C,D)local S=BaseShot:Clone()S.CFrame=m+m.lookVector*(BaseShot.Size.Z/2)local j=Instance.new("\066\111\100\121\086\101\108\111\099\105\116\121")j.velocity=m.lookVector*SHOT_SPEED j.Parent=S S.Touched:connect(function(e)I(S,e)end)DebrisService:AddItem(S,SHOT_TIME)S.Parent=Tool end Tool.GripPos=Vector3.new(0,.2,-0.5)Tool.GripRight=Vector3.new(.926,.2,-0.377)wait(.1)Tool.GripPos=Vector3.new(0,.2,-0.25)Tool.GripRight=Vector3.new(.926,.15,-0.377)wait(.1)Tool.GripPos=Vector3.new(0,.2,0)Tool.GripRight=Vector3.new(.926,.1,-0.377)wait(.1)Tool.GripPos=Vector3.new(0,.2,.125)Tool.GripRight=Vector3.new(.926,.05,-0.377)wait(.1)Tool.GripPos=Vector3.new(0,.2,.25)Tool.GripRight=Vector3.new(.926,0,-0.377)wait(.25)ReloadSound:Play()wait(1.25)Tool.Enabled=true end end local function P() 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)
		for i = 1, 25 do
			local target = Humanoid.TargetPoint
			target = CFrame.Angles(
				(0.5 - math.random()) * 2 * (math.random() * 0.04),
				(0.5 - math.random()) * 2 * (math.random() * 0.04),
				(0.5 - math.random()) * 2 * (math.random() * 0.04)
			) * target

			local shotCFrame = CFrame.new(firingPoint, target)

			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
		end

		Tool.GripPos = Vector3.new(0, 0.2, -0.5)
		Tool.GripRight = Vector3.new(0.926, 0.2, -0.377)
		wait(0.1)
		Tool.GripPos = Vector3.new(0, 0.2, -0.25)
		Tool.GripRight = Vector3.new(0.926, 0.15, -0.377)
		wait(0.1)
		Tool.GripPos = Vector3.new(0, 0.2, 0)
		Tool.GripRight = Vector3.new(0.926, 0.1, -0.377)
		wait(0.1)
		Tool.GripPos = Vector3.new(0, 0.2, 0.125)
		Tool.GripRight = Vector3.new(0.926, 0.05, -0.377)
		wait(0.1)
		Tool.GripPos = Vector3.new(0, 0.2, 0.25)
		Tool.GripRight = Vector3.new(0.926, 0, -0.377)
		wait(0.25)

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

		Tool.Enabled = true
	end
end

local function OnUnequipped() end
