This requires looking at several surrounding verses for context.
Matthew 24:23 For there shall arise false Christs, and false prophets, and shall shew great signs and wonders; insomuch that, if it were possible, they shall deceive the very elect. 25 Behold, I have told you before. 26 Wherefore if they shall say unto you, Behold, he is in the desert; go not forth: behold, he is in the secret chambers; believe it not. 27 For as the lightning cometh out of the east, and shineth even unto the west; so shall also the coming of the Son of man be. 28 For wheresoever the carcase is, there will the eagles be gathered together.
29 Immediately after the tribulation of those days shall the sun be darkened, and the moon shall not give her light, and the stars shall fall from heaven, and the powers of the heavens shall be shaken: 30 And then shall appear the sign of the Son of man in heaven: and then shall all the tribes of the earth mourn, and they shall see the Son of man coming in the clouds of heaven with power and great glory. 31 And he shall send his angels with a great sound of a trumpet, and they shall gather together his elect from the four winds, from one end of heaven to the other. 32 Now learn a parable of the fig tree; When his branch is yet tender, and putteth forth leaves, ye know that summer is nigh: 33 So likewise ye, when ye shall see all these things, know that it is near, even at the doors. 34 Verily I say unto you, This generation shall not pass, till all these things be fulfilled. 35 Heaven and earth shall pass away, but my words shall not pass away. 36 But of that day and hour knoweth no man, no, not the angels of heaven, but my Father only. 37 But as the days of Noah were, so shall also the coming of the Son of man be. 38 For as in the days that were before the flood they were eating and drinking, marrying and giving in marriage, until the day that Noe entered into the ark, 39 And knew not until the flood came, and took them all away; so shall also the coming of the Son of man be.
So, what was Jesus saying in verse 34? First, we have to look at what He said just before that. What are "these things" that would indicate that His coming was "near, even at the doors" and that had to be fulfilled before "this generation" would pass? Things like false Christs and false prophets showing "great signs and wonders; insomuch that, if it were possible, they shall deceive the very elect". And things like what are described in verse 29. Those things are probably figurative (stars falling from heaven does not seem like a literal thing at all to me), but whatever they represent would be among "these things" that would indicate that His coming is "near, even at the doors". Luke describes those things as "those things which are coming on the earth" (Luke 21:26). Jesus seems to be focused on a high level of deception and such as being what will indicate that His coming is near moreso than anything else. Which lines up with what Paul taught in 2 Thessalonians 2:1-12.
His second coming is a global event (2 Peter 3:10-13) and hasn't happened yet, so therefore "this generation" cannot be referring to the chronological generation living at the time Jesus gave the Olivet Discourse. It has to be that whatever "this generation" means, it can't pass away until the things that happen to indicate that Christ's second coming are near happen first. Once those things happen then "this generation" will pass away and even "heaven and earth shall pass away" (verse 35).
So, what is "this generation" referring to then? The word "generation" is translated from the Greek word "genea" and it has several different definitions. The one that I believe fits here without causing any contradictions is that it's referring to a kind of people. The Jews, in particular. So, I see Jesus as saying the Jews, as a people, would not pass away until the things that happen just before His second coming happen first. At that point, even "heaven and earth will pass away". This is when what is described in 2 Peter 3:10-12 will happen. That's why Jesus said in Matthew 24:38-39 that just as the flood "took them all away", meaning that it killed all unbelievers, "so shall also the coming of the Son of man be. All unbelievers will be destroyed when Jesus returns as well.