Sediment stealing capacity
A 50 gallon tank with four inches of hardened sediment on the floor is not a 50 gallon tank any more. You lose usable volume and recovery speed at once.
A flush restores some of it, if it is done before the sediment fully hardens.
A failed lower element
Electric heaters have two elements. When the lower one fails, the upper element still heats the top third of the tank, so you get hot water - just not much of it.
This is the single most common cause of a shower that goes cold after five minutes, and it is an inexpensive repair.
A broken dip tube
The dip tube carries incoming cold water to the bottom of the tank. When it cracks or breaks off near the top, cold water mixes straight into the hot outlet.
The symptom is lukewarm water that never gets properly hot, rather than hot water that runs out.
The heater is simply too small
Households change. A 40 gallon tank that suited two people does not suit four with a soaking tub. No repair fixes undersizing.
This is where either a larger tank or a tankless unit is the honest answer, and where a plumber should tell you both prices before you decide.