How should I be using BindToClose()?

What’s the connection between them? IsStudio checks if the current playing environment is in the studio or not. I don’t see anything related to this.

I have seen 0 reports about this.

You use RunService:IsStudio() to ignore the BindToClose call. Because if you don’t, both player removing ,and bind to close, they will fire twice. Because you stopped the server, and you left at the same time.

1 Like

I still don’t see the connection between them. This only checks if the playtesting is in Roblox Studio. If it is, it WON’T save the data, otherwise in games, it will.

Bro. It already has the normal PlayerRemoving one which WILL fire.

The point of the :IsStudio() is to prevent the :BindToClose() function from firing in Studio. It will still fire in regular servers.

1 Like

And I don’t see the problem with saving data twice?

It can give you a warning about data stores being over-loaded;

1 Like

When there’s only 1 player in a server? I rememebr that a maximum call of data stores is only 60 if only 1 player is in the server.

1 Like

Another thing I notice is, even though you have IsStudio on, it still will save the data twice if a player is in the game. When that last player left, the PlayerRemoving event and the BindToClose() will still be fired at the same time.

Yeah but multiple warnings all the time about data stores are annoying in studio.

To actually answer the question, I would just save the data and not kick the player. This method is also the only way to go about it if you’re trying to do a soft shutdown.

Which call? Because you’re checking if the environment is not in studio when you’re in a game server, so that conditional statement passes and the BindToClose event is connected.

This code example will prevent the data from being saved twice if there is only one player left in the server:

local RunService = game:GetService("RunService")

if not RunService:IsStudio() then
    game:BindToClose(function()
        if #game.Players:GetPlayers <= 1 then return end

        -- your data saving code here
    end)
end
3 Likes

this is the best way to save the player’s data on BindToClose().

game:BindToClose(function()
   if not RunService:IsStudio() and #Players:GetPlayers() > 1 then
     for _, player in ipairs(Players:GetPlayers()) do
          coroutine.wrap(yourSaveFunction)(player)
     end
   end
end)
3 Likes

Quick question, if you just kicked everyone, and it automaticly starts saving because you kicked them, since its mass kicking, would’t the datastores start stop excepting requests due to so many set requests?

1 Like

Every data store call is individual for each player but global for the server, that’s why you have limited requests per players, 60 + numPlayers * 10 (i think that’s the formula to calculate how many requests)

Data Store Errors and Limits (roblox.com)

I think thats the case for get requsets, unless your saying you have to wait 6 seconds per set reqeust for a key, which then you can do multiple set requests, im not sure
https://developer.roblox.com/en-us/articles/Datastore-Errors

GetAsync doesn’t have a cooldown only SetAsync & UpdateAsync +

Sorry for the late respoese, and yes i know, but i guess my question is how are you supppossed to save data with bind to close if you have a 6 second cool down for setasynces and update asyncs, so how exactly are you suppossed to save all that data because you could only save 3 players data since bindtoclose only allows 30 seconds. Is there a 6 second cool down per datastore key?

The code above uses coroutine to save the player’s data at the same time and its going to save because the cooldown is individual per key requests.

for example: if you call UpdateAsync on the same key 6 times you’ll get a queue limit warning but if you call it one time per key each 6 seconds the data will be saved but you will be using too many budget.