Fix endless cycle problem when using script