The most likely bad connection would be in the white neutral wire because that’s the only one that would affect both the lights and the fan motor. Turn off the power and open the switch box and lower the fan so you can reach the wiring. Remove the wire nuts and make sure all the white wires are even at the bare end and twisted tightly together. I’m fairly sure you will find the wires in the fan uneven or maybe not even touching when you remove the wire nut. If you have to, restrip the wires carefully and twist them together again before you put the wire nut back on. Hang the fan again and test it again. Hopefully the second try will fix the problem.