Studio randomly deleting the saved data and not loading

As it says in the title, data store randomly deleted all of my Leaderstats data while I was testing my game in Roblox Studio, I got no error, and I have previously tested it multiple times without changing the scripts and nothing was wrong, then decided to load in one more time, and that’s when it all reset to 0. I was just wondering if this would happen in the real game, if so, how can I prevent it?

3 Likes

Please provide scripts. We can’t help you if we don’t know anything about the internals of your system.

local DataStore = game:GetService("DataStoreService")
local PlayerData = DataStore:GetDataStore("PlayerData")

local function onPlayerJoin (player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash" 
	Cash.Parent = leaderstats
	
	local Diamonds = Instance.new("IntValue")
	Diamonds.Name = "Diamonds"
	Diamonds.Parent = leaderstats
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Parent = leaderstats
	
	
	local playeruserid = "Player_"..player.UserId
	local data = PlayerData:GetAsync(playeruserid)
	if data then 
		Cash.Value = data["Cash"]
		Diamonds.Value = data["Diamonds"]
		Rebirths.Value = data["Rebirths"]
	else
		Cash.Value = 0
		Diamonds.Value = 0
		Rebirths.Value = 0
		
	end
end
local function Create_Table(player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onplayerexit(player)
	local player_stats = Create_Table(player)
	local success, err = pcall(function()
		local playeruserid = "Player_"..player.UserId
		PlayerData:SetAsync(playeruserid, player_stats)
	end)	
	
	if not success then
		warn("error saving")
	end
	if success then
		print(player.Name.." Saved")
	end
end
game.Players.PlayerRemoving:Connect(onplayerexit)
game.Players.PlayerAdded:Connect(onPlayerJoin)

it is a simple data store script

It just, out of no where deleted the saved data

You should be using update async instead of set async. Set async is prone to data loss.

1 Like

Thank you for that, I will change it, and if it ever looses it again I will reply again to your comment.

UpdateAsync is mostly useful when you need both old and new data, there is no problem using SetAsync, you just need to use it correctly.

How should I use SetAsync correctly?

There might be a chance that roblox studio itself isn’t actually deleting your data, but rather roblox studio not being able to save your data in time before it stops testing. This sort of issue happened for me too. This might not entirely be the issue, however if it is then your best shot at preventing it is by having the game autosave whatever data you have so you don’t have to entirely rely on said player leaving to save anything.

1 Like

Thanks for that Idea, I will definitely be adding that.

1 Like
local DataStore = game:GetService("DataStoreService")
local PlayerData = DataStore:GetDataStore("PlayerData")

local function onPlayerJoin (player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash" 
	Cash.Parent = leaderstats
	
	local Diamonds = Instance.new("IntValue")
	Diamonds.Name = "Diamonds"
	Diamonds.Parent = leaderstats
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Parent = leaderstats
	
	
	local playeruserid = "Player_"..player.UserId
	local data = PlayerData:GetAsync(playeruserid)
	if data then 
		Cash.Value = data["Cash"]
		Diamonds.Value = data["Diamonds"]
		Rebirths.Value = data["Rebirths"]
	else
		Cash.Value = 0
		Diamonds.Value = 0
		Rebirths.Value = 0
		
	end
end
local function Create_Table(player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onplayerexit(player)
	local player_stats = Create_Table(player)
	local success, err = pcall(function()
		local playeruserid = "Player_"..player.UserId
		PlayerData:SetAsync(playeruserid, player_stats)
	end)	
	
	if not success then
		warn("error saving")
	end
	if success then
		print(player.Name.." Saved")
	end
end
game:BindToClose(function()
	for _, player in next, game.Players:GetPlayers() do
		onplayerexit(player)
	end
end)
game.Players.PlayerRemoving:Connect(onplayerexit)
game.Players.PlayerAdded:Connect(onPlayerJoin)

in this code i simply added game:BindToClose. (might save the data twice but i suppose it won’t do anything wrong)

thanks for telling me.

1 Like

Thanks, I will also be adding this to my script.

1 Like

Not entirely sure how reliable this change would be, then again saving data twice might also mean the data not being saved for some players too due to the fact that those data store functions do require waiting between each time you use them. And even IF you use them. they still actually take some time to save anything after using said functions

I don’t full understand what you mean by saving the data twice might mean that the data is not being saved.

Basically same reason for why your data might not be saved in roblox studio. Each call the DataStoreService takes a bit of time and on top of that an even longer wait time for you to be able to use said functions again. Saving data twice for every single player will just make the entire process magnitudes slower and since all the data saving is done when the server/studio test is being turned off, its very likely that said data will not finish saving for all players before the server/studio test is actually fully stopped.

1 Like

I just thought, what if the problem is in the loading of the data, is their anything wrong with that in my script.

I mean personally, i cannot really see any sort of evident flaw in your own code as of now.

Ok well thanks for your help, if you find anything just let me know.

1 Like

Update async is generally a better practice due to the fact that Roblox datastores do not have 100% uptime. It is impossible to prevent data loss and so in the event that things go wrong on Roblox’s end you should be prepared.

1 Like