How can we bypass an infinite loop?

I have a light that I want to turn on and off every 5 seconds. I tried doing this with a while loop and a delay but I got the infinite loop error. Is there a way to approach having something go on forever using BP?

You should use a timeline for that. Read about timelines here.

You can just use a “Set Timer” with a Custom Event. Take a look at this example.

This is an easy mistake to make; what you need to understand about (code) loops is that they attempt to run all of their iterations EVERY TICK. That means that even if, theoretically, Unreal let you run an infinite loop, you’d be unable to play the game since the engine would just enter the loop and perform the same task over and over without refreshing the frame or doing anything else (i.e. your game would simply freeze), since there would be no way for the game thread to complete its task and then enter the next tick.

What you want instead is to use a timeline, as trdwll said; timelines fire off pulses and/or change values based on the duration of time when triggered, while the game is running (rather than in between frames). You can set a timeline to “loop” and it will repeat itself over and over; this is the kind of looping you want, not a code loop, which is more useful for repeating the same set of instructions a number of times (e.g. performing the same task for every variable in an array) as soon as it’s called.