Pausing sometimes makes the print fail

Sometimes when my Enclosure plugin pauses my print due lack of filament or I just pause it to inspect something. after resuming it either just goes in one direction extruding a line and jamming itself(I assume getting stuck on a single gcode?) or one time it was extruding a dot going up extruding and then going up until I pulled the plug to avoid jamming it to the top of the frame!

I use Octopi on my raspberry pi 3 with my anycubic i3 mega-s

OctoPrint -> Settings -> Gcode Scripts includes two scripts which run before/after the pause. If you share them here then we might be able to advise you.

Safely pausing/resuming is technically a difficult problem since a sliced gcode file might have absolute/relative X/Y/Z as well as absolute/relative extrusion/retraction. So it's hard for a one-size-fits-all pair of pause/resume scripts at this moment in time since the printer's firmware currently won't report its current mode(s).

To improve my success in this area, I always use relative extrusion as a Cura slicer option and leave the X/Y/Z movement mode to absolute. As a result of this, my pause/resume scripts then may operate under this set of assumptions.

It's empty so default!

Sounds like you might need a pair of scripts if you don't like how it's currently behaving, right?

Research a little, learn from what they did, understand the commands and implement something.