How can I loop this to create the skeleton of an auto-battler/Idle game

Well, we don’t know what you want, so it’s hard to say what would be required for it. It also isn’t clear where you imagine using the loop, what it would loop over, or how you intend for it to contribute to the project.

Please read the pinned thread to understand how to post code in the forum with proper formatting. We shouldn’t have to go to a third-party site for this. Then try to explain what issue you are running into with the existing code: does it give a wrong result? Is it missing functionality? Something else?