A bad experience (everywhere..) can be arriving fast .. you go to the wrong places. .meet bad people. .get rip off.. and for sure ..the way you would describe your trip is not terrific!
I would say. that the very first thing to do is to get informed and get some good advices about the place you are looking forward to visit.
You can find a huge quantity of information if you carefully look on TA's forum and posts.
A part of it, depends of your expectations and if you are at the right place at the right moment regarding what you could call a perfect trip!
Concerning security, I would say again & again that the Tourist brigade is really doing a great job here and as said many times. .you get here less chance to be bothered than in Paris & London.. but can happen my friends!
You might just use your "common sense" and avoid pick pocket and other nuisance especially the ones who propose their service to take you places to places and just desire having commission on your back or make cash in a bad way!
..and when something bad happen. .if small.. don't take it as a full catastrophe and save your irony!
(Very) last.. Usually ..people love Marrakech ..or hate but no middle of the road!