How should I be using BindToClose()?

So I have seen two methods for BindToClose().

game:BindToClose(function()
    for i, plr in pairs(game.Players:GetPlayers()) do
        plr:Kick() -- Apparently so that it fires the PlayerRemoving function?
    end
end

and

game:BindToClose(function()
    for i, plr in pairs(game.Players:GetPlayers()) do
        saveData() --Saving the players data on BindToClose; I have problems with this one saying there's too many requests.
    end
end

I’m using this for a module, which one should I use?

8 Likes

I believe I saw AlvinBlox doing the :Kick() method.

1 Like

I’m sorry but this sounds pointless, if it’s a server wipe button then both methods should work fine.

1 Like

No, it’s for basic use, aka saving the players data when the server goes down;

And from what I’ve heard, but 'im not sure, kicking the player would fire the PlayerRemoving, and there fore not calling this twice when on testing on studio;

Use player.removed for saving data it should work even in a server crash, otherwise autosave.

What’s the point of connecting the PlayerRemoving event with the kicked player?

If your going to argue with everyone who has a different opinion, what’s the point of this?

3 Likes

so THAT MY DATA STORE saves, I don’t get what you mean? Aren’t you supposed to save player data on BindToClose() in case a server goes down?

3 Likes

Then why are you still asking when you know BindToClose() is suppose to save all player’s data?

1 Like

Yes, but everyone has already given you the answer.

3 Likes

I’m confused no? What method should I use? HERES THE THING

I’ve seen people just :Kick() all players when a server goes down, aparrently so that it fires the PlayerRemoving event.

And I’ve seen people also just save their data. And not do that.

1 Like

If your game is so unstable that you have specialized scripts ready for immediate action when your server crashes then i don’t think you should be asking for a good data saving but rather why it’s so unstable.

1 Like

… That’s pointless because when the server is shut down and then the player gets kicked not because of the Kick() function, the PlayerRemoving event should be fired as well.

1 Like

That’s not a good method. You shouldn’t do that, it’s sort of hacky. Refer to the post above:

It’s not unstable. This is recommended by ROBLOX.

Listen, don’t worry about this function. When a player no matter what leaves the game or is kicked/disconnected then player.Removed will fire everytime no exceptions.

It’s not really about a player being disconnected, it’s about a server crashing.

Now we are getting so confused. Server crashing for saving data? If it’s that, then the second option is already the answer.

If you’re worried about that, then you can just use something like this:

local RunService = game:GetService("RunService")

if not RunService:IsStudio() then
    game:BindToClose(function()
        -- your data saving code
    end)
end
1 Like

What?

If a server crashes, you might not be disconnected instantly. You might disconnect later when the server IS GONE. There fore, not saving your data. Kicking would disconnect immediately and fire a player removing immediately, but saving the data could also work the same way, but could fire twice.

If that happends, BINDTOCLOSE can help you with that. But, like the person above said, it’s better to use RunService:IsStudio() because bind to close fires even when theres only one player and he leaves.