DataStores2 Not Saving When Game Crashes or Shuts down

I’m just going to get straight into whats going on.

My issue is that DataStores2 is not saving when the game shuts down or when a player crashes, yet it saves when I leave the server and rejoin which is super confusing.

I’m new to DataStores2 since I’ve started learning them about 2 or 3 days ago, I’ve also watch a few tutorials. I have asked for help on my issue about 4 times and nobody knows what the problem is, and as a last resort I’m asking help on the dev forums.
When I say nobody knows what the issue is, I mean literally nobody knows.

Here is my code:

local dataStore2 = require(1936396537)
local defaultCash = 0
game.Players.PlayerAdded:Connect(function(plr)
    local pointsDataStore = dataStore2("points",plr)
    local leaderstats = Instance.new("Folder",plr)
    leaderstats.Name = "leaderstats"
    local points = Instance.new("IntValue",leaderstats)
    points.Name = "Points"
    local function pointsUpdate(updateValue)
        points.Value = pointsDataStore:Get(updateValue)
    end
    pointsUpdate(defaultCash)
    pointsDataStore:OnUpdate(pointsUpdate)
    
end)

First impressions about using DataStores2 from this alone is not good and I’m already considering going back to normal DataStores, I hope somebody knows the answer to my problem.
Id also like to say thanks in advance for anybody who spends any time trying to help me.

3 Likes

This looks like the code from @Alvin_Blox’s video about Datastore2. Whenever he makes a video, it is most likely that there wouldn’t be any errors.

Please give more information, a video should help.

My only guess is that you are expecting the Datastore to work in studio, which it doesn’t, really. And, are you sure you have changed the data?

1 Like

I’ve tested datastores2 in normal servers many times and like I’ve said before that datastores2 only saves when you leave the game, not when you disconnect or when the server shuts down.

iirc it does (should) save when the server shuts down ( using bindtoclose), so this shouldn’t happen , could this be related to this? if it is , this shouldn’t be the case anymore if you’re using the latest version.


Yeah ive made sure that im using the latest version of the datastores2, I tried using the bind to close thing and it still doesnt save upon shutdown.

did you get the version from Git?, someone said that’s the only version that is maintained but I have no idea if that’s true or not.

EDIT: is this happening ingame or in studio?

1 Like

yea im using the one from github and im testing this in game

oh then i’m not sure, I don’t really use Datastore2 tbh, but you’ll probably have to look for more information on the Datastore2 article, you could be doing something wrong OR a new ds2 update is broken.

have you set the data first? I don’t think you can update it unless it exists first.

everything works normally as a datastore2 would work
is just that its not saving when the server crashes or disconnects but it loads when you leave the game and rejoin

1 Like

you could probably save using

--fires when server is closing.
game:BindToClose(function()
    --save all players here with a coroutine
end)
Click to see the BindToClose code that I normally use
        game:BindToClose(function()
		DataManager:StopAutoSaving()
		local thread = coroutine.running()
		local saveAll = coroutine.wrap(function()
			DataManager:SaveAll()
		end)
		spawn(function()
			saveAll()
			coroutine.resume(thread)
		end)
		coroutine.yield(thread)
	end)

I thought datastore2 did this automatically though?

Having this same issue months later, did anyone find a fix yet? I was under the assumption that DataStore2 saves any data the player might have had during that session when they leave / crash but that does not seem to be happening within my games.

Any feedback from @Kampfkarren would help here, thanks!

Hello,

I’m having this issue right now. Did you find a solution?

Having the same data loss issue, but I was able to fix the :BindToClose() not being called just by moving it out of the__call() function and just moved it further down the script. So that fixed data not saving when shutting down servers, but now having a new issue where the player’s data isn’t saving when they crash. Which is odd because I feel like even if they crash it would either run bindtoclose or playerRemoving to save their data, but for some reason it doesnt? They end up losing hours of progress when they crash, which really sucks :// Did anyone find a solution to data not saving when the players crash??