Just had a datastore loss

So i just was playtesting my game on the actual roblox platform then i noticed all of my datas (vanilla datastore) got wiped out :slightly_frowning_face: i never made a single change to the datastore leaderstats script or anything the only thing ive changed was a standalone different script that shows how much ā€œCashā€ you have in a TextLabel and thatā€™s it.
Is there any way to prevent this type of datastore losses? :frowning_face: if so, any ideas you donā€™t mind sharing? because i seriously donā€™t want this happen to again.
Iā€™m guessing is it due to the game servers shutting down? :frowning_face:

2 Likes

You should use ProfileService or Datastore2.

1 Like

Mind sharing the script with us?

1 Like

Here it is:

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerData")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local TIX = Instance.new("IntValue")
	TIX.Name = "TIX"
	TIX.Value = 0
	TIX.Parent = leaderstats
	
	
	local data = nil
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.UserId.."-TIX", player.leaderstats.TIX.Value) 
	end)

	if success and data ~= nil then 
		TIX.Value = data
	else
		player:Kick(errormessage.. " We had trouble getting datastore! oops!")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		playerData:SetAsync(player.UserId.."-TIX", player.leaderstats.TIX.Value)
	end)

	if success then
		print("Data successfully saved!")
	else
		print("There was an error while saving the data")
		warn(errormessage)
	end

end)
1 Like

Iā€™m trying to migrate to datastore2 but the process seems too tedious.

1 Like

A few things:
UpdateAsync > SetAsync;
Use game:BindToClose(function() -- Stuff end), this allows you to save data when the server begins shutting down.

1 Like

Will this work? Itā€™s on the same leaderstats script

game:BindToClose(function(player)
	local success, errormessage  = pcall(function()
		playerData:SetAsync(player.UserId.."-TIX", player.leaderstats.TIX.Value)
	end)
	if success then
		--Send discord webhook saying player.Name is saved
	else
		--Opposite of earlier.
	end
end)
1 Like

It doesnā€™t return a player as an argument, you want to use the Players service to iterate through all the players in-game (via <Players>:GetPlayers()) and then save it.

1 Like

Hi!

Iā€™ve made a direct guide on how to implement DataStore2, in a thread where a person had issues with saving leaderstats. Itā€™s almost plug and play. :slight_smile:

2 Likes