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

	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
