--------------------------------------------------------------------------

function Explode() --This function makes the plane explode
	if (not Crashed.Value) then
		Crashed.Value = true
		Plane:BreakJoints()
		local Explosion = Instance.new("Explosion")
		Explosion.Parent = game.Workspace
		Explosion.Position = Engine.Position
		Explosion.BlastRadius = Plane:GetModelSize().magnitude / 2 --This makes the BlastRadius the size of the plane
		Explosion.BlastPressure = 2e5
		if Engine:findFirstChild("Thrust") then Engine.Thrust:Destroy() end
		if Engine:findFirstChild("Direction") then Engine.Direction:Destroy() end
		delay(5,function() Plane:Destroy() end) --This removes the plane after 5 seconds
	end
end

function GetPlaneParts(Model) --This function gets all the parts on the plane
	for _,v in pairs(Model:GetChildren()) do
		if (v:IsA("BasePart")) then
			table.insert(Parts,v) --This inserts all the parts into the "Parts" table
		end
		GetPlaneParts(v)
	end
end

function onAutoCrash() --This function is activated when the AutoCrash value becomes true
	if AutoCrash.Value then
		Explode()
	end
end

function DetectExplosions(Object) --This is a function that I created which detects explosions in the workspace
	if Object.ClassName == "Explosion" then --If the object detected was an explosion...
		if Object.BlastPressure > 0 then
			local Dist = (Object.Position - Engine.Position).magnitude
			if Dist <= (Object.BlastRadius + 2) then
				if (not Crashed.Value) then
					Explode()
				end
			end
		end
	end
end

function IsObstacle(Obj) --This function determines whether Obj is an obstacle that can be used to destroy the plane
	if (not Obj:IsDescendantOf(Plane)) and Obj.CanCollide then
		local PlaneTag = Obj:FindFirstChild("PlaneTag")
		if (PlaneTag and PlaneTag.Value ~= Plane) or (not PlaneTag) then
			return true
		end
	end
	return false
end

function IsDescendantOfPlayer(Obj)
	for _,v in pairs(game.Players:GetPlayers()) do
		if v and v.Character then
			if Obj:IsDescendantOf(v.Character) then
				return v
			end
		end
	end
	return nil
end

GetPlaneParts(Plane) --This activates the "GetPlaneParts" function
for _,v in pairs(Parts) do --This gets all the parts in the "Parts" table
	v.Touched:connect(function(Object) --This is activated if any of the bricks are touched
		if (not Ejected.Value) then
			if CanCrash.Value and (not Crashed.Value) then
				if IsObstacle(Object) then
					local Player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
					if Player then
						local HitPlayer = IsDescendantOfPlayer(Object)
						if HitPlayer == Player then return end
					end
					if (v.Velocity.magnitude >= Force.Value) then
						Explode()
					end
				end
			end
		elseif Ejected.Value then
			if (not Crashed.Value) then
				if IsObstacle(Object) then
					if Object.Parent.Name ~= "EjectorSeat" then
						if (not IsDescendantOfPlayer(Object)) then
							Explode()
						end
					end
				end
			end
		end
	end)
end

game.Workspace.DescendantAdded:connect(DetectExplosions) --The detect explosions is a DescendantAdded function
AutoCrash.Changed:connect(onAutoCrash) --Pretty self explanatory. When the AutoCrash value is changed, the "onAutoCrash" function is activated
