Some clarity on game:BindToClose()

Let’s say I have a custom DataStore queue, with a loop going through all requests that waits for GetRequestBudgetForRequestType() to allow the request.

And let’s say that in the same Script I want to add game:BindToClose() to allow the extra 30 seconds for the requests before the server shuts down.

If I just do this:

game:BindToClose( function()
    wait( 30 )
end )

Will it allow the rest of the script to keep running (the queue loop), or do I explicitly need to add what I want to do inside BindToClose() ?

2 Likes

“Multiple functions can be bound using BindToClose if it is called repeatedly. The game will wait a maximum of 30 seconds for all bound functions to complete running before shutting down. After 30 seconds, the game will shut down regardless if all bound functions have completed or not.”

Therefore, you canno’t attempt to extend the time it takes the server to shut down.

I’m not asking about extending the time though, I’m asking if I have to place all the code I want to run on shut-down inside BindToClose, or can I simply put some sort of wait inside BindToClose (30 seconds, or until some condition is met - assuming a 30 second cap ofc), that will allow the rest of the script to keep running for that time.

Oh, in that case, no you shouldn’t wait 30 seconds before executing code when the server is about to close.

BindToClose runs when the server shuts down, and waits 30 seconds until all functions get done. Nothing will be done afterwards, the code will just stop. You CAN’T have yields that last more than 30 seconds, you can run any code in BindToClose

Guys, I don’t think you’re getting me here.

I am not asking if I can wait 30 seconds BEFORE executing some code, obviously that wouldn’t make sense as I have 30 seconds until the server shuts down. And yes I understand that I have a 30 second window and that’s it, if the code isn’t done, if some data has not saved until then - it’s lost.

Let me reiterate my question:

Imagine you have a Script that is doing something, going through some loop, and then all players leave, the server starts shutting down and BindToClose is called. Once BindToClose is called, will that loop terminate, and only what is specified inside BindToClose keeps running, OR is it possible to simply put wait( 30 ) inside BindToClose and know that the loop outside of BindToClose will keep running until the server shuts down?

It’s probably best if you have another function that can override that queue loop that links to BindToClose() using wait at the max time of 30 seconds isn’t a good practice, bind a function to close and immediately complete what you need to ASAP, prevent “hanging” data in your data stores or missing data.

In theory provided you aren’t spamming data stores every half-second to second or so (see data store limits) you shouldn’t need to use GetRequestBudgetForRequestType(), but in certain cases I can see it being useful, and being good practice to use it.

But yes, you could use bindtoclose with a wait to possibly have that other function keep running up to 30 seconds.

1 Like

Thank you!

I know I shouldn’t need GetRequestBudget generally, but I like to go for the worst-case scenario, especially when it comes to purchases.

When spamming purchases as fast as I can there is an issue with throttling, which for some reason charges the account without granting the item. And while I know the chance of someone doing purchases this rapidly is close to nothing, if someone for some reason does it I don’t want them to lose their money.

2 Likes