you could probably use a Boolean flag for this.
local isSafeToClose = true --global, at the top of your script
local function thisMustFinish()
isSafeToClose = false
--logic
isSafeToClose = true
end
--then
game:BindToClose(function()
repeat task.wait() until isSafeToClose
end)
note that BindToClose
can only run for a maximum of 30 seconds… so you basically have a 30s time limit after the game is set to close