What you should setup is called Safe Z. It is under General Config. No script needed.