You may want to use Self for this type of script, as it deals better with persisting state. Refer to the Loop script which already does something similar, https://www.botlibre.com/script?id=482051