local function s()local s=Instance.new("\080\097\114\116")s.Name="\082\111\099\107\101\116"s.FormFactor=Enum.FormFactor.Custom s.Size=ROCKET_PART_SIZE s.CanCollide=false s.BottomSurface=Enum.SurfaceType.Smooth s.TopSurface=Enum.SurfaceType.Smooth local k=Instance.new("\083\112\101\099\105\097\108\077\101\115\104",s)k.MeshId=ROCKET_MESH_ID k.Scale=ROCKET_MESH_SCALE k.TextureId=ToolHandle.Mesh.TextureId local r=Instance.new("\070\105\114\101",s)r.Heat=10 r.Size=5 local z=Instance.new("\082\111\099\107\101\116\080\114\111\112\117\108\115\105\111\110",s)z.CartoonFactor=1 z.TargetRadius=TARGET_RADIUS z.MaxSpeed=MAX_SPEED z.MaxTorque=MAX_TORQUE z.MaxThrust=MAX_THRUST z.ThrustP=THRUST_P z.ThrustD=THRUST_D local t=SwooshSound:Clone()t.Parent=s local w=BoomSound:Clone()w.Parent=s local T=Instance.new("\079\098\106\101\099\116\086\097\108\117\101",s)T.Name="\099\114\101\097\116\111\114"T.Value=MyPlayer local d=Instance.new("\083\116\114\105\110\103\086\097\108\117\101",T)d.Name="\105\099\111\110"d.Value=Tool.TextureId local O=RocketScript:Clone()O.Parent=s O.Disabled=false return s end local function k()MyModel=Tool.Parent MyPlayer=PlayersService:GetPlayerFromCharacter(MyModel)BaseRocket=s()RocketClone=BaseRocket:Clone()end local function r(s)if Tool.Enabled and(MyModel and(MyModel:FindFirstChild("\072\117\109\097\110\111\105\100")and MyModel.Humanoid.Health>0))then Tool.Enabled=false local k=MyModel.Humanoid.TargetPoint if s then k=MyModel.Humanoid.Torso.CFrame.lookVector*1000 end local r=ToolHandle.Position+ToolHandle.CFrame.lookVector*(ToolHandle.Size.z/2)RocketClone.CFrame=CFrame.new(r,k)DebrisService:AddItem(RocketClone,30)RocketClone.Parent=Workspace FireSound:Play()local z=RocketClone:FindFirstChild("\082\111\099\107\101\116\080\114\111\112\117\108\115\105\111\110")if z then local s=(k-RocketClone.Position).unit z.TargetOffset=RocketClone.Position+s*TARGET_OVERSHOOT_DISTANCE z:Fire()end wait(0)local t=RocketClone:FindFirstChild("\083\119\111\111\115\104")if t then t:Play()end RocketClone=BaseRocket:Clone()ReloadSound:Play()wait(COOLDOWN)ReloadSound:Stop()Tool.Enabled=true end end local function z()ReloadSound:Stop()end


local function MakeBaseRocket()
	-- Set up the rocket part
	local rocket = Instance.new('Part')
	rocket.Name = 'Rocket'
	rocket.FormFactor = Enum.FormFactor.Custom --NOTE: This must be done before changing Size
	rocket.Size = ROCKET_PART_SIZE
	rocket.CanCollide = false
	rocket.BottomSurface = Enum.SurfaceType.Smooth
	rocket.TopSurface = Enum.SurfaceType.Smooth

	-- Add the mesh
	local mesh = Instance.new('SpecialMesh', rocket)
	mesh.MeshId = ROCKET_MESH_ID
	mesh.Scale = ROCKET_MESH_SCALE
	mesh.TextureId = ToolHandle.Mesh.TextureId

	-- Add fire
	local fire = Instance.new('Fire', rocket)
	fire.Heat = 10
	fire.Size = 5

	-- Add the propulsion
	local rocketPropulsion = Instance.new('RocketPropulsion', rocket)
	rocketPropulsion.CartoonFactor = 1
	rocketPropulsion.TargetRadius = TARGET_RADIUS
	rocketPropulsion.MaxSpeed = MAX_SPEED
	rocketPropulsion.MaxTorque = MAX_TORQUE
	rocketPropulsion.MaxThrust = MAX_THRUST
	rocketPropulsion.ThrustP = THRUST_P
	rocketPropulsion.ThrustD = THRUST_D

	-- Clone the sounds
	local swooshSoundClone = SwooshSound:Clone()
	swooshSoundClone.Parent = rocket
	local boomSoundClone = BoomSound:Clone()
	boomSoundClone.Parent = rocket

	-- Attach creator tags
	local creatorTag = Instance.new('ObjectValue', rocket)
	creatorTag.Name = 'creator' --NOTE: Must be called 'creator' for website stats
	creatorTag.Value = MyPlayer
	local iconTag = Instance.new('StringValue', creatorTag)
	iconTag.Name = 'icon'
	iconTag.Value = Tool.TextureId

	-- Finally, clone the rocket script and enable it
	local rocketScriptClone = RocketScript:Clone()
	rocketScriptClone.Parent = rocket
	rocketScriptClone.Disabled = false

	return rocket
end

local function OnEquipped()
	MyModel = Tool.Parent
	MyPlayer = PlayersService:GetPlayerFromCharacter(MyModel)
	BaseRocket = MakeBaseRocket()
	RocketClone = BaseRocket:Clone()
end

local function OnActivated(byFireButton)
	if Tool.Enabled and MyModel and MyModel:FindFirstChild('Humanoid') and MyModel.Humanoid.Health > 0 then
		Tool.Enabled = false

		-- Get the target position
		local targetPosition = MyModel.Humanoid.TargetPoint
		if byFireButton then -- Using Fire Button, shoot forwards
			targetPosition = MyModel.Humanoid.Torso.CFrame.lookVector * 1000
		end

		-- Position the rocket clone
		local spawnPosition = ToolHandle.Position + (ToolHandle.CFrame.lookVector * (ToolHandle.Size.z / 2))
		RocketClone.CFrame = CFrame.new(spawnPosition, targetPosition) --NOTE: This must be done before assigning Parent
		DebrisService:AddItem(RocketClone, 30)
		RocketClone.Parent = Workspace

		-- Assign target and launch!
		FireSound:Play()
		local rocketPropulsion = RocketClone:FindFirstChild('RocketPropulsion')
		if rocketPropulsion then
			local direction = (targetPosition - RocketClone.Position).unit
			rocketPropulsion.TargetOffset = RocketClone.Position + (direction * TARGET_OVERSHOOT_DISTANCE)
			rocketPropulsion:Fire()
		end

		wait(0) --TODO: Remove when sounds realize they can be played as soon as they enter the Workspace

		-- Swoosh!
		local swooshSound = RocketClone:FindFirstChild('Swoosh')
		if swooshSound then
			swooshSound:Play()
		end

		-- Prepare the next rocket to be fired
		RocketClone = BaseRocket:Clone()

		ReloadSound:Play()

		wait(COOLDOWN)

		-- Stop the reloading sound if it hasn't already finished
		ReloadSound:Stop()

		Tool.Enabled = true
	end
end

local function OnUnequipped()
	ReloadSound:Stop() --TODO: This does not work online
end
