*Btw, I'm assuming you have an L96* From what it sounds like, it may be that the spring that holds the sear up might have been dislodged, causing the spring not to catch. The reason why you're able to cock your gun while on safety is because even though the spring may not be there, the safety holds the sear in the right position. To be 100% sure, I'm going to need you to remove your trigger box from the upper receiver and take it apart to see if everything is in working order. Then make sure that it is put back together similar to this one:
Now, this is just intuition, but here is exactly what I believe is going on. The spring "1" might not be in place causing "2" to go down which prevents it from stopping the sear "3" from going forward and results in not cating the piston when cycling the bolt back. When the safety is engaged it goes on top of the nub at "4" which would hold "2" in its place preventing it from dropping even if the spring is not there. Make sure your trigger box is assembled right and you shouldn't have to worry about this problem. If all else fails and you find that your trigger box is in fact broken, then you can go on amazon and buy a UTG Metal Trigger Box.

Now, this is just intuition, but here is exactly what I believe is going on. The spring "1" might not be in place causing "2" to go down which prevents it from stopping the sear "3" from going forward and results in not cating the piston when cycling the bolt back. When the safety is engaged it goes on top of the nub at "4" which would hold "2" in its place preventing it from dropping even if the spring is not there. Make sure your trigger box is assembled right and you shouldn't have to worry about this problem. If all else fails and you find that your trigger box is in fact broken, then you can go on amazon and buy a UTG Metal Trigger Box.