Rotary encoder menu system

It's something to do with the fact I'm using the 'buttonPressed' flag from outside of the button press code.

This works:

// Push button variables
const byte buttonPin = 4; // Arduino pin the push button is connected to.
byte previousButtonState = HIGH;  // Assume switch open because of pull-up resistor.
byte currentButtonState; // Will be populated by reading button pin satae.
const unsigned long debounceTime = 10;  // Milliseconds of debounce.
unsigned long buttonPressTime;  // Time since button last changed state. Used to differentialte button bounce vs. intentional press.
boolean buttonPressed = 0; // A flag variable to identify button presses. Returned from button debounce code. 

void setup() {
  pinMode (buttonPin, INPUT_PULLUP); // Set button pin as input.
  Serial.begin(9600); // Begin Serial monitor.
}

void loop() {
  
  // Button press code with debounce.
  currentButtonState = digitalRead (buttonPin); 
  if (currentButtonState != previousButtonState){
    if (millis () - buttonPressTime >= debounceTime){ // debounce
      buttonPressTime = millis ();  // when we closed the switch 
      previousButtonState = currentButtonState;  // remember for next time 
      if (currentButtonState == LOW){ // When debounce criteria are met and button is still pressed.
        buttonPressed = 1; // Set button flag to 1.
        Serial.println("Button Pressed");
      }
      else { // When debounce criteria are not met because boutton is not pressed or it is bouncing.
        buttonPressed = 0;  // Set button flag to 0.
      }
    }
  }
  
 
}

This doesn't:

// Push button variables
const byte buttonPin = 4; // Arduino pin the push button is connected to.
byte previousButtonState = HIGH;  // Assume switch open because of pull-up resistor.
byte currentButtonState; // Will be populated by reading button pin satae.
const unsigned long debounceTime = 10;  // Milliseconds of debounce.
unsigned long buttonPressTime;  // Time since button last changed state. Used to differentialte button bounce vs. intentional press.
boolean buttonPressed = 0; // A flag variable to identify button presses. Returned from button debounce code. 

void setup() {
  pinMode (buttonPin, INPUT_PULLUP); // Set button pin as input.
  Serial.begin(9600); // Begin Serial monitor.
}

void loop() {
  
  // Button press code with debounce.
  currentButtonState = digitalRead (buttonPin); 
  if (currentButtonState != previousButtonState){
    if (millis () - buttonPressTime >= debounceTime){ // debounce
      buttonPressTime = millis ();  // when we closed the switch 
      previousButtonState = currentButtonState;  // remember for next time 
      if (currentButtonState == LOW){ // When debounce criteria are met and button is still pressed.
        buttonPressed = 1; // Set button flag to 1.
     
      }
      else { // When debounce criteria are not met because boutton is not pressed or it is bouncing.
        buttonPressed = 0;  // Set button flag to 0.
      }
    }
  }
  
   if(buttonPressed == 1){
   Serial.println("Button Pressed");
   }
}