How do you fix this code to prevent "Not running script because past shutdown deadline"?

Here’s a partial code of my event handler script. Most of the time when I exit out of studio, the process takes a lot of time to kill the running scripts. One of the running scripts affects the process and takes it longer to shut down. I found out that it was my event handler. I wasn’t sure why this happened because a couple months ago, I never seen this happened, but I wanted to point this out because it kept bugging me out.

-- idleTimeout: number (optional)
-- runService: game:GetService'RunService'
-- readyToReturn: boolean -> this variable is marked after one of the specified event handlers is done
-- wait: task.wait

local stWait = os.time()
repeat
	wait()
	if not runService:IsRunning() then break end
until
	(idleTimeout and os.time()-stWait > idleTimeout) or readyToReturn

In the code, I added a break loop if the game isn’t running anymore, but somehow it still affects studio.

How do I fix this issue?

The real question is why do you have this code in your game? I’m not surprised at all that such error occurs. Aren’t you looking for something like game:BindToClose? Otherwise, explain what you’re trying to achieve with this because it’s certainly not the right way to do so.