Both script snippets that are shared here is affected by the issue described here:
Please apply the workaround and give us the feedback if you are still having issues!