Catapult reload system not working

Hi, my bot has a catapult for launching discs, so the idea of my code is to launch some discs and then bring the catapult back to a specific position. That specific position is marked with a limit switch, so the code is supposed to start the match with the catapult down in the load position, hitting the limit switch, and when a button is pressed, shoot the catapult and automatically keep spinning the motor until it hits the limit switch and is back in the loading position. When the code is run, the catapult immediately goes into the loading position, but when the button is pressed, the catapult shoots repeatedly. I was wondering if someone could help me out. Thanks in advance!

Here is an example of what the code looks like for reference:

bool shooting = Controller1.ButtonX.pressing();
bool loaded = false;

// If we aren't actively shooting, then run the motor to load it
while (!loaded && !shooting) {
  if (LimitSwitch.pressing()) {
    loaded = true;
    Catapult.stop(hold);
  } else {
    Catapult.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
  }
}
// If we are shooting then the catapult motor stays running until the limit switch is pressed.
while (shooting) {
  Catapult.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
}

It looks like you aren’t using while loops right. Your current code will get if the button is pressed once, go down until the limit switch is pressed, and then spin the motor infinitely. What you should do instead is replace the while loops you have with if statements, then wrap the whole code you sent in a while (true), with a wait(20, msec) at the end. Here’s a template to work off of:

void drivercontrol() {
    while (true) {
        bool shooting = Controller1.ButtonX.pressing();

        if (/* limit switch not pressed */) {
            // spin catapult down
        }

        if (/* button x pressed */) {
            // spin catapult down
        }

        wait(20, msec);
    }
}