Sensor loosing values.

My scan void prints a value scanned from sensor. but he is loosing digits for some reason prints less characters the he is suposed to. when he is scanning he is running this loop.

do {
    
  unsigned int rgbColour[3];

  // Start off with red.
  rgbColour[0] = 255;
  rgbColour[1] = 0;
  rgbColour[2] = 0;  

  // Choose the colours to increment and decrement.
  for (int decColour = 0; decColour < 3; decColour += 1) {
    int incColour = decColour == 2 ? 0 : decColour + 1;

    // cross-fade the two colours.
    for(int i = 0; i < 255; i += 1) {
      rgbColour[decColour] -= 1;
      rgbColour[incColour] += 1;
      
      setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);

      scan();

      delay(5);
    }
   }
  } while (ledloop == "on");

What exactly do you mean by "loosing digits"? It's hard to tell what is wrong in scan() because you didn't include it in your sketch. You also didn't include the declarations of most of your variables. Or your setup() or loop() functions. Please include the entire sketch is you want help.

while (ledloop == "on"); This won't work, unless ledloop is a String (not advisable with Arduino).

You should be using character arrays and strcmp().

const int redPin = 6;
const int greenPin = 5;
const int bluePin = 3;

String ledloop = "on";
String scanresult = "";

#define STX 0x02
#define ETX 0x03

const size_t LEN = 24; // length of the expected message (e.g. 12 hex characters = 12 * 2 = 24)


char buffer[LEN+1];
int index = 0;
bool receiving = false;



void setup() {
 // Start off with the LED off.
 setColourRgb(0,0,0);
   Serial.begin(9600);
 Serial1.begin(9600);

}

void loop() {


 do {
   
 unsigned int rgbColour[3];

 // Start off with red.
 rgbColour[0] = 255;
 rgbColour[1] = 0;
 rgbColour[2] = 0;  

 // Choose the colours to increment and decrement.
 for (int decColour = 0; decColour < 3; decColour += 1) {
   int incColour = decColour == 2 ? 0 : decColour + 1;

   // cross-fade the two colours.
   for(int i = 0; i < 255; i += 1) {
     rgbColour[decColour] -= 1;
     rgbColour[incColour] += 1;
     
     setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
     scan();
     delay(5);
   }
  }
 } while (ledloop == "on"); 



}




void scan() {

if (Serial1.available() > 0) {
   char serialByte = Serial1.read();
   if (serialByte == STX) {
     receiving = true;
     index = 0;
   } else if (serialByte == ETX || index == LEN) {
     buffer[index] = '\0';
     parse(buffer, index);
     index = 0;
     receiving = false;
   } else if (receiving) {
     buffer[index++] = serialByte;
   }
 }

 
 }


void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
 analogWrite(redPin, red);
 analogWrite(greenPin, green);
 analogWrite(bluePin, blue);
}


void parse(char* input, size_t length) {

 scanresult = input;
 Serial.println(scanresult);
 
 
}

How much data is coming in on Serial1?

With a delay of 5 milliseconds in main loop, your scan function will execute less than 200 times a second. If data arrives faster, buffer will overflow.

26 characters per read

And how many read per second?

edit
Try change if to while. It will make sure you read everything from the buffer.

void scan() {

while (Serial1.available() > 0) {

non stop reading

You are reading one character at a time, and with a delay(5) between every read you can read less than 200 characters per second.
With 26 characters per reading it about 7.7 readings per second. More and buffer will fill up and you lose data.

void scan() {

while (Serial1.available() > 0) {

i made that change and now it works fine. do i need to clean buffer or something similar?