Judges will decide custody matters based on what is in the best interest of the child.  The more issues that a parent has, the more likely a judge is to deny their request for custody.  Issues like drug abuse and mental health issues will certain impact that decision.  The timing, the extent, and severity of each will have a bearing on the denial of custody and any limitations placed on visitation.  As long as the other parent does not have similar or worse issues, then the other parent would have a much better chance of obtaining custody.  However, because the test for "best interest of the child" is a factor based test, no one can completely predict for you what the judge will ultimately do.