People who invest in the stock market are a bit like people who visit the casino. They'll tell you about a gamble they made that paid off many times over. What they won't tell you is all of the money they lost on other bets. I've bought plenty of stocks in my life and some have been great. Some have not been great. In then end, most people are better off putting their money in an index fund and getting the average return.
Investing in the stock market, especially through a 401k with a company match is smart. However, it does not have the advantages of real estate. Consider leverage. If you put $20k down and borrow $80k on a $100k property, a 10% increase in the asset's value is $10k. That is a 50% ROI. If that same money gained 10% in the stock market, your ROI would be 10%. If it's a rental property, the tenants are paying the mortgage, so even without strong capital appreciation, your tenants are slowly but surely paying down your debt and building equity for you. It doesn't seem like a lot at first, but over time, it's magical. What's more, any income you receive is highly tax advantaged due to depreciation.
To give an example, we have a property this year that will cash flow about $40k. On that we will get a $24k depreciation deduction. Thus, we are only paying tax on $16k of the cash flow. It's not that good on all of our properties, but I wouldn't say this is unusual for a highly leveraged property. Suppose a tax rate of 25% and instead of an after tax net of $30k for W-2 or stock income, we get a net of $36k. Honestly it feels like cheating but the law is the law. You are required to take depreciation.
All that being said, this business is very hard work and the risks are very real. It helps to have a lot of patience and to be prepared for the worst because we all have setbacks.