local function j()local j=RocketTemplate:Clone()j.PrimaryPart.CFrame=CFrame.new(Configurations.StoragePosition.Value)j.Parent=Storage j.PrimaryPart:SetNetworkOwner(Owner)if Owner then j.PrimaryPart.BrickColor=Owner.TeamColor end table.insert(Buffer,j)end local function H()for H=1,Configurations.BufferSize.Value,1 do j()end end local function p(j)for H=1,#Buffer,1 do if Buffer[H]==j then table.remove(Buffer,H)return end end end local function e()for j,H in ipairs(Buffer)do H.PrimaryPart:SetNetworkOwner(Owner)if Owner then H.PrimaryPart.BrickColor=Owner.TeamColor end end end local function A(j,H)local e=Players:GetPlayerFromCharacter(j.Parent)or Players:GetPlayerFromCharacter(j.Parent.Parent)if not e then if j.Parent.Name~="\082\111\099\107\101\116\076\097\117\110\099\104\101\114"and(j.Parent.Name~="\070\108\097\103"and not j.Anchored)then local e=(j.Size.X*j.Size.Y)*j.Size.Z if e<=Configurations.MaxDestroyVolume.Value then if H<Configurations.BlastRadius.Value*Configurations.DestroyJointRadiusPercent.Value then j:BreakJoints()end if j.Parent.Name=="\082\111\099\107\101\116"then p(j.Parent)end if#j:GetChildren()==0 then wait(2*e)j:Destroy()end end end end end local function M(j,H,p)local e=Instance.new("\069\120\112\108\111\115\105\111\110",game.Workspace)e.ExplosionType=Enum.ExplosionType.NoCraters e.DestroyJointRadiusPercent=0 e.BlastRadius=Configurations.BlastRadius.Value e.BlastPressure=Configurations.BlastPressure.Value e.Position=p e.Hit:connect(function(j,p)A(j,p,H)end)H.Part.Fire.Enabled=false wait(1)H:Destroy()end local function G(j)if j=="\080\097\114\101\110\116"then if Tool.Parent.Name=="\066\097\099\107\112\097\099\107"then local j=Tool.Parent Owner=j.Parent elseif Players:GetPlayerFromCharacter(Tool.Parent)then Owner=Players:GetPlayerFromCharacter(Tool.Parent)else Owner=nil end e()end end local function n(H,p)p.Rocket.Transparency=0 p.Part.Fire.Enabled=true j()end local function q(j,H,p)local e=Players:GetPlayerByUserId(tonumber(j))if e.TeamColor==H.TeamColor and Configurations.FriendlyFire.Value or e.TeamColor~=H.TeamColor then local j=e.Character:FindFirstChild("\072\117\109\097\110\111\105\100")if j then j:TakeDamage(p)end end end local function W(j,H)for H,p in pairs(H)do q(H,j,Configurations.SplashDamage.Value)end end local function o(j,H)q(H,j,Configurations.Damage.Value)end

local function CreateRocket()
	local rocketCopy = RocketTemplate:Clone()
	rocketCopy.PrimaryPart.CFrame = CFrame.new(Configurations.StoragePosition.Value)
	rocketCopy.Parent = Storage
	rocketCopy.PrimaryPart:SetNetworkOwner(Owner)
	if(Owner) then
		rocketCopy.PrimaryPart.BrickColor = Owner.TeamColor
	end
	
	table.insert(Buffer, rocketCopy)
end

local function IntializeBuffer()	
	for i = 1, Configurations.BufferSize.Value do
		CreateRocket()
	end
end

local function RemoveFromBuffer(rocket)
	for i = 1, #Buffer do
		if Buffer[i] == rocket then
			table.remove(Buffer, i)
			return
		end
	end
end

local function ResetRocketOwner()
	for _, rocket in ipairs(Buffer) do
		rocket.PrimaryPart:SetNetworkOwner(Owner)
		if(Owner) then
			rocket.PrimaryPart.BrickColor = Owner.TeamColor
		end
	end
end

local function OnExplosionHit(part, distance)
	local player = Players:GetPlayerFromCharacter(part.Parent) or Players:GetPlayerFromCharacter(part.Parent.Parent)
	if not player then
		if part.Parent.Name ~= 'RocketLauncher' and part.Parent.Name ~= 'Flag' and not part.Anchored then
			local volume = part.Size.X * part.Size.Y * part.Size.Z
			if volume <= Configurations.MaxDestroyVolume.Value then
				if distance < Configurations.BlastRadius.Value * Configurations.DestroyJointRadiusPercent.Value then
					part:BreakJoints()
				end
				if part.Parent.Name == "Rocket" then
					RemoveFromBuffer(part.Parent)
				end
				if #part:GetChildren() == 0 then
					wait(2 * volume)
					part:Destroy()
				end
				
			end
		end
	end
end

local function OnRocketHit(player, rocket, position)
	local explosion = Instance.new('Explosion', game.Workspace)
	explosion.ExplosionType = Enum.ExplosionType.NoCraters
	explosion.DestroyJointRadiusPercent = 0
	explosion.BlastRadius = Configurations.BlastRadius.Value
	explosion.BlastPressure = Configurations.BlastPressure.Value
	explosion.Position = position
	explosion.Hit:connect(function(part, distance) OnExplosionHit(part, distance, rocket) end)
	rocket.Part.Fire.Enabled = false
	wait(1)
	rocket:Destroy()
end

local function OnChanged(property)
	if property == 'Parent' then
		if Tool.Parent.Name == 'Backpack' then
			local backpack = Tool.Parent
			Owner = backpack.Parent
		elseif Players:GetPlayerFromCharacter(Tool.Parent) then
			Owner = Players:GetPlayerFromCharacter(Tool.Parent)
		else
			Owner = nil
		end
		ResetRocketOwner()
	end		
end

local function OnFireRocket(player, rocket)
	rocket.Rocket.Transparency = 0
	rocket.Part.Fire.Enabled = true
	CreateRocket()
end

local function DamagePlayer(hitPlayerId, player, damage)
	local hitPlayer = Players:GetPlayerByUserId(tonumber(hitPlayerId))
	if (hitPlayer.TeamColor == player.TeamColor and Configurations.FriendlyFire.Value) or hitPlayer.TeamColor ~= player.TeamColor then
		local humanoid = hitPlayer.Character:FindFirstChild('Humanoid')
		if humanoid then
			humanoid:TakeDamage(damage)
		end
	end
end

local function OnHitPlayers(player, hitPlayers)
	for hitPlayerId, _ in pairs(hitPlayers) do
		DamagePlayer(hitPlayerId, player, Configurations.SplashDamage.Value)
	end
end

local function OnDirectHitPlayer(player, hitPlayerId)
	DamagePlayer(hitPlayerId, player, Configurations.Damage.Value)
end
