How to use DataStore2:BindToClose?

Okay, so i want to know how to use DataStore2:BindToClose() function on DataStore2 Module so when game shutdowned the players data got saved

1 Like

I would just use the normal roblox datastore. If scripted correctly, you won’t have any data loss.

No, it’s better to use storing engines such as Datastore2 or even ProfileService, that i very like. Those engines make sure the data is saved while never encountering key requests filling. There is no excuse to not use them.

1 Like

I can’t get my head wrapped around how to use them. I find it hard honestly, that’s why I use the normal datastore. Have had no issues what so ever, its just how you set the datastore up.

DataStore2 already creates its own BindToClose handler, you don’t need to do that yourself. The BindToClose method exposed by DataStore2 allows you to write some additional handlers with data. All handlers are called with the player and the value last set in the cache.

You can check arguments with varargs:

local DummyData = DataStore2(Dummy, "Foobar")

DummyData:BindToClose(function (...)

You probably don’t need this either though. Your use case is saving player data when BindToClose fires but like I said DataStore2 already takes care of that for you.

1 Like

but when i shutdown my game it doesnt save players data

Could you give more context here? How deeply have you debugged your code - are you absolutely sure that DataStore2 is problematic here and not potentially the way you’re using DataStore2 or your other code that’s changing data? Where are you testing this? Need way more details here.

1 Like

i dont make when PlayerRemoving and save the data with datastore2 but it automatic save when PlayerRemoving but i dont know why when game is BindToClose it dont save all players data in the server

Hey which is correct to use :Save() on DataStore2

DataStore2(“Stats”, player):Save()

which is correct?

It does look overwhelming at first but with only 1 good youtube tutorial you’ll understand all of it. Try profileService, it’s very good and not hard at all to use once you learned it. Good luck!

1 Like