BindToClose: your wildest dreams will come true!

Hello, friend!

How many times has this happened to you!?!?!?

-- somewhere in your lua code...
game.OnClose = function()
    ImportantCleanupFunction1()
end

-- somewhere else in your lua code
game.OnClose = function()
    ImportantCleanupFunction2()
end

You try and try, but no matter what you do only one of the ImportantCleanupFunctions gets called!!

Sure, you could make one function that calls all of your other functions. But that leads to awkward code interdependencies, bloating, cramps, and unsightly stretch marks.

Oh, can’t someone DO something?!??!

Yes, ROBLOX can!! Introducing the amazing new “game.BindToClose”. With game.BindToClose, you can:

  • Bind an arbitrary number of functions to the close event!
  • See your bound functions execute in parallel on game end!
  • Marvel as the game waits until all of your bound functions finish (assuming they finish within 30 seconds)!
  • Rest assured that if there’s a game.OnClose function, it will be called (in parallel with other calls).

You may say, “Gee, that sounds great. But won’t it be confusing to use?”

No WAY, Juanita! Check it out:

game:BindToClose(function()
	print("Starting callback...")
	wait(5)	
	print("Done callback...")
end)

Just listen to this real life testimonial!

“Before game.BindToClose, I was a real mess. My code was convoluted and I couldn’t use third party code for fear that they’d override my OnClose function. Now, with game.BindToClose, everything is coming up roses! Licked those intestinal problems to boot!"
–BobStars

Available in your API NOW!!

88 Likes

Now we just wait and hope those third parties update their code very soon to use BindToClose.

7 Likes

Cool, I can scrap 20 lines of my codebase now

And people can finally make free models/modules which use OnClose and it won’t mess up other running code (such as that datastore module on the wiki)

Yeeeeeee

tbh @CycloneUprising should write all the update announcements

27 Likes

50% hype from the update, 50% hype from the way the post was written!

14 Likes

i :heart: this writing

16 Likes

Maybe I’m just slow but I could really use a practical example of why this is useful. Im always discovering new API in ROBLOX I’ve never seen before so hearing most of what was said here has me boggled a little.

1 Like

For data management a bit:
(making sure some module that saves playerdata has enough time to actually save data)

game:BindToClose(function()
	while PlayerData:StillSavingDataOfSomeone() do wait(1) end
end)

or analytics:

game:BindToClose(function()
	HttpService:PostAsync(url.."/serverclosed",game.PlaceId..","..game.JobId)
end)

just random code that should give you a few ideas

2 Likes

You made a lot of people feel a sigh of relief…
Thank you.

6 Likes

All updates should be documented in this format.

4 Likes

Ok I get it now, thanks.

I have awaited this for a while. Now my systems are more compatible with games. :smiley:

1 Like