DataStore sometimes does not load?

Hello there. I currently have a very simple data store set up that saves and loads a players currency. Now I should mention that this is my first game using data stores so keep judging down to a minimum if the error is basic. So the problem is that sometimes players will just lose their stats and the stats will go back to the previous stats. For example I tested with 500 currency, I put it to 1000. I left and it said I had 1000 (in the saving pcall), but once I joined the game it said I had 500. I set up printing the data store errors in the output for saving and loading but nothing showed up… Any help would be great, thank you.

--Loading currency
local function loadCurrency(player)
	local currency
	local success, err = pcall(function()
		currency = currencyDataStore:GetAsync(player.UserId)
	end)
	print(currency)
	if success and currency ~= nil then
		player.leaderstats["Sanity Points"].Value = currency
	end
end



--Saving currency
local function saveCurrency(player)
	local success, err = pcall(function()
		print(player.leaderstats["Sanity Points"].Value)
		currencyDataStore:SetAsync(player.UserId, player.leaderstats["Sanity Points"].Value)
	end)
end






game.Players.PlayerAdded:Connect(function(player)
	local teleportingValue = Instance.new("BoolValue",player)
	teleportingValue.Name = "Teleporting"
	
	local lobbyTheme = Instance.new("StringValue",player)
	lobbyTheme.Name = "LobbyTheme"
	lobbyTheme.Value = ""
	
	local themesFolder = Instance.new("Folder", player)
	themesFolder.Name = "BunkerThemes"
	
	local classicTheme = Instance.new("StringValue", themesFolder)
	classicTheme.Name = "Classic"
	
	local currencyValue = Instance.new("NumberValue", player:WaitForChild("leaderstats"))
	currencyValue.Name = "Sanity Points"
	
	local productBoughtCheck = Instance.new("BoolValue", player)
	productBoughtCheck.Name = "ProductBoughtCheck"
	
	game.ReplicatedStorage.Events:FireClient(player)
	
	task.spawn(loadThemes, player)
	task.spawn(loadCurrency, player)
	
	game.ReplicatedStorage.NewPlayer:FireClient(player)
end)





game.Players.PlayerRemoving:Connect(function(player)
	task.spawn(saveThemes, player)
	task.spawn(saveCurrency, player)
end)


game:BindToClose(function()
	for _, plr in pairs(game.Players:GetChildren()) do
		task.spawn(saveThemes, plr)
		task.spawn(saveCurrency, plr)
	end
end)

Are you attempting to save/load data from Studio?

Ahhh yes. I believe that was the problem. I think whenever I was exiting the test session the game would sometimes not save before the server shutdown. I tested it with adding a wait(5) in the game:BindToClose and so far no issues

I’ve had issues with data saving in Studio. I believe this is caused by the server shutting down too quickly. If you publish the game and try to save data through the game client, it will likely save and load properly. If that’s the case, you won’t need to wait in the BindToClose function. It’s always better to avoid waiting in BindToClose because you have only about 30 seconds before the server completely terminates itself.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.