How to fix Google play services has stopped problem

I recently flashed my tablet with a newer Android version that wasn’t available over the air. I kept getting an error “Google play services has stopped” every few seconds. I tried clearing cache and data, but it wouldn’t go away.

What actually did work for me is going to Settings -> Apps -> Show system apps -> Google play services and giving it all the permissions. Apparently it was freaking out because it was lacking some permission.