Working with laser sensor module, light receiver sensor, keypad and servo motor

I want to create a software program that when the light receiver sensor detects something, a message will appear saying,“Hello, please enter a 4 digit code to enter”. After putting the 4 digit code, servo motor will move 90 degrees, wait 1-3 seconds and then move back to initial position, it will appear a message saying “Password correct, you may pass”. If code is incorrect, message will say “Password incorrect, please enter again the 4 digit password” and servo will not move. So far this is what I have on code:

#include <Servo.h>
#include <Keypad.h>

#define Rec 0
#define Laser 2
Servo myservo;


const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char keymap[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keymap), rowPins, colPins, ROWS, COLS); 
char customKey;
char code[5];
char master_code[5] = "5943";
byte index = 0;


void setup() {
  Serial.begin(9600);
  Serial.print("System Status:Normal");
  myservo.attach(12);  // attaches the servo on pin 9 to the servo object
  pinMode(Laser, OUTPUT);
  digitalWrite(Laser, HIGH);
  delay(2000);
  
}

void loop() {
  customKey = customKeypad.getKey();
  short Detect = analogRead(Rec);
  if(Detect < 300){
    Serial.println("");
    Serial.print("Hello, please input the 4 digit password to enter");
    Serial.print("");
  }
     else{
    Serial.print("");
  }
  if (customKey){
    code[index] = customKey;
    index++;
    Serial.print(customKey);
  }
  if (index ==4){
    if(!strcmp(code,master_code)){
    Serial.print("");
    Serial.println("Password Correct, you may pass");
    Serial.print("");
    myservo.write(90);
    delay(1000);
    myservo.write(0);
    }
    else{
      Serial.println("Password Incorrect, please enter again the 4 digit password");
    }
  }
 
  

}

The electronic parts all work perfectly, I just need guidance on making the code program. These are my issues:
1.- When light receiver detects something, the message is repeated in multiple lines vertically and I want just to be printed once, I already put serial.print and it is repeated horizontally. Same problem goes to the other serial. print on the void loop part of the code.
2.- When you put the incorrect password and then you put the password correct, program ends and servo doesnt move.
3.-When you put the correct password, servo motor moves 90 degrees but continue moving from 0 to 90 degrees on a loop.

For the electronics, I am using a laser sensor module (KY-008), light sensor module, 4x4 keypad and servo motor

What you describe is a series of sequential events so you don't need to continually call loop(). Do it more in series.

If the password is not correct, you do not reset index back to 0 to start getting more input.
You also do not terminate the input with a '\0' so it is not a string and you can not use strcmp() to compare it.

So, should I use different void() calls to make it more in series or how exactly? And in regards to the index part should I just put it this way more or less?:

if (index ==4){
    if(!strcmp(code,master_code)){
    Serial.print("");
    Serial.println("Password Correct, you may pass");
    Serial.print("");
    myservo.write(90);
    delay(1000);
    myservo.write(0);
    }
    else{
       index == 0;
      Serial.println("Password Incorrect, please enter again the 4 digit password");
    }
  }

This should be close

#include <Servo.h>
#include <Keypad.h>

const byte RecPin =  A0;
const byte LaserPin = 2;
Servo myservo;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char keymap[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keymap), rowPins, colPins, ROWS, COLS);

const char master_code[] = "5943";
const int codeLength = sizeof(master_code) / sizeof(master_code[0]);

enum { CORRECT, INCORRECT, TIMEOUT }; // values returned from getPassword()

const unsigned long timeout = 10000;  // amount of time without input before resetting

void setup() {
  Serial.begin(9600);
  Serial.print("System Status:Normal");
  myservo.attach(12);  // attaches the servo on pin 9 to the servo object
  pinMode(LaserPin, OUTPUT);
  digitalWrite(LaserPin, HIGH);
  delay(2000);
}

void loop() {
  Serial.println("Waiting for detection..." );

  while ( analogRead(RecPin) > 300) {
    // do nothing
    delay(25);
  }

  bool done = false;
  while ( !done ) {
    int result = getPassword();

    if (result == CORRECT) {
      Serial.print("");
      Serial.println("Password Correct, you may pass");
      Serial.print("");
      myservo.write(90);
      delay(1000);
      myservo.write(0);
      delay(1000);
      done = true;
    }
    else if ( result == INCORRECT ) {
      Serial.println("Password Incorrect, please enter again the 4 digit password");
    }
    else {
      // timeout so we are done
      done = true;
    }
  }
}

int getPassword() {
  unsigned long startTime = millis();
  int index = 0;
  char code[codeLength];
  bool done = false;
  int result = INCORRECT;

  Serial.println("");
  Serial.print("Hello, please input the 4 digit password to enter");

  while ( !done ) {
    char customKey = customKeypad.getKey();
    if (customKey) {
      code[index] = customKey;
      index++;
      Serial.print(customKey);
    }
    if (index == codeLength - 1) {
      code[index] = '\0';
      if (strcmp(code, master_code) == 0) {
        result = CORRECT;
      }
      done = true;
    }
    if ( millis() - startTime >= timeout ) {
      // timeout happened
      result = TIMEOUT;
      done = true;
    }
  }
  return result;
}

Do I have to declare a function named getPassword() at the start of the code program? Around this area?:

#include <Servo.h>
#include <Keypad.h>

const byte RecPin =  A0;
const byte LaserPin = 2;
Servo myservo;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char keymap[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keymap), rowPins, colPins, ROWS, COLS);

const char master_code[] = "5943";
const int codeLength = sizeof(master_code) / sizeof(master_code[0]);

enum { CORRECT, INCORRECT, TIMEOUT }; // values returned from getPassword()

const unsigned long timeout = 10000;  // amount of time without input before resetting

Is marking me an error saying "'getPassword' was not declared in this scope". I think with that it seems theoretically program may work

not if you are using the IDE. It auto generates function declarations for you.

@bih64 The code you sent work perfect, thank you very much. I just have a minor issue, when I put the wrong password it appears two messages "Password incorrect, please enter againi the 4 digit password" and "Hello, please input the 4 digit password to enter". How can it just only print out the message "Password incorrect, please enter againi the 4 digit password"

#include <Servo.h>
#include <Keypad.h>

const byte RecPin =  A0;
const byte LaserPin = 2;
Servo myservo;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char keymap[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keymap), rowPins, colPins, ROWS, COLS);

const char master_code[] = "5943";
const int codeLength = sizeof(master_code) / sizeof(master_code[0]);

enum { CORRECT, INCORRECT, TIMEOUT }; // values returned from getPassword()

const unsigned long timeout = 10000;  // amount of time without input before resetting

void setup() {
  Serial.begin(9600);
  Serial.print("System Status:Normal");
  myservo.attach(12);  // attaches the servo on pin 12 to the servo object
  myservo.write(90);
  pinMode(LaserPin, OUTPUT);
  digitalWrite(LaserPin, HIGH);
  delay(2000);
}

void loop() {
  Serial.println("Waiting for detection..." );

  while ( analogRead(RecPin) > 300) {
    // do nothing
    delay(25);
  }

  bool done = false;
  while ( !done ) {
    int result = getPassword();

    if (result == CORRECT) {
      Serial.print("");
      Serial.println("Password Correct, you may pass");
      Serial.print("");
      myservo.write(0);
      delay(5000);
      myservo.write(90);
      delay(1000);
      done = true;
    }
    else if ( result == INCORRECT ) {
      Serial.println("");
      Serial.println("Password Incorrect, please enter again the 4 digit password");
      Serial.println("");
    }
    else {
      // timeout so we are done
      done = true;
    }
  }
}

int getPassword() {
  unsigned long startTime = millis();
  int index = 0;
  char code[codeLength];
  bool done = false;
  int result = INCORRECT;

  //Serial.println("");
  Serial.println("Hello, please input the 4 digit password to enter");

  while ( !done ) {
    char customKey = customKeypad.getKey();
    if (customKey) {
      code[index] = customKey;
      index++;
      //Serial.println("");
      Serial.println(customKey);
      //Serial.print("");
    }
    if (index == codeLength - 1) {
      code[index] = '\0';
      if (strcmp(code, master_code) == 0) {
        result = CORRECT;
      }
      done = true;
    }
    if ( millis() - startTime >= timeout ) {
      // timeout happened
      result = TIMEOUT;
      done = true;
    }
  }
  return result;
}

P.S. i apologize replying so late, I had to download again Arduino IDE, had some issues with it

I am very confident you can figure it out. What have you tried? What message to you want to print?