How can i improve this DataSave script?

Hello, after having different problems with datasave I changed my script and I made this one which seems already better than the old one, everything works very well. However: I looked on the internet and I could understand through some posts that my script is globally bad because there is neither DataStore2/Corounes/ProfileService (and other things that I don’t know/maintain) which makes me doubt. I’ve finished my game now but I’m not publishing it because I’m afraid of having data leaks after a few days. Could someone tell me if it is possible to improve this script ? And how? I tried Courones but with the table I use I didn’t succeed.
Or maybe my script is safe ? So i don’t know i’m lost with all these informations

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

local function saveData(player)
	local cTable = {}

	for _, creature in pairs(player:WaitForChild("Creatures"):GetChildren()) do
		table.insert(cTable,creature.Name)
	end

	local success, errorMsg = pcall(function()
		DataStore:SetAsync(player.UserId, cTable)
	end)

	if success then
		print("data")
	else 
		print("no data")
		warn(errorMsg)

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

	local data
	local success, errorMsg = pcall(function()
		data = DataStore:GetAsync(player.UserId)
	end)	
	if success and data then
		for _, cName in pairs(data) do
			if workspace.Creatures:FindFirstChild(cName) then 
				local newval = Instance.new("BoolValue")
				newval.Name = cName
				newval.Parent = folder
				print("data2")
			else 
				print("no data2")
			end	
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMsg = pcall(function()
		saveData(player)
	end)
	if success then
		print("data3")
	else 
		print("no data3")
	end
end)

game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers()) do
		local success, errorMsg = pcall(function()
			saveData(plr)
		end)
		if success then
			print("data4")
		else 
			print("no data4")

		end
	end
end)
2 Likes

even if the improvement of the script is minimal it will already know that and I will be happy. I would like to understand what are the probabilities of losing my data? Does it happen frequently? Because it’s happens it will be the death of the game insured this kind of problem

Please I don’t ask for much. Just to know if from your knowledge I can improve my script and if so how? Otherwise is this script above likely to have data memoy problems ? thank you very much

I did have data loss issues initially in my game, which used a similar saving system as yours.

The solution that fixed all of these happened to be DataStore2, which you mentioned in your post. Is there any reason you want to avoid DataStore2?

Thank you for your answer
No particular reason, I just need to find out how it all works because I have never used this data. So you use two scripts, right? A datasave1 and a datasave2

I have read on the forum by many people that the use of the DataStore2 is not nearly as necessary as what is said everywhere. From the script above ? Someone could tell me how I could improve it. Thank you

A couple simple improvements to add could be:

  1. A way to delete saved data (For when you get a right-of-erasure-request from a player)
  2. Data store history to revert to last save version
  3. A retry method in case the datastore fails to retry getting the data
  4. A way to kick a player if the datastores fail, so they don’t override/lose previous data
1 Like

Thank it make sense. All this can be done on the basis of my script without anything else? Or do I have to add modules and other things

Player kick seems to be good for you ? Do you know which module i have to add to data history, and why i have to delete data if a player resquet for ? Thank you. I really appreciate your help i need it

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "Creatures"
	folder.Parent = player
	
	local data
	local success, errorMsg = pcall(function()
	data = DataStore:GetAsync(player.UserId)
end)
	local savedata = Instance.new("BoolValue", player)
	savedata.Name = "SaveData"
	savedata.Value = true	
       if success and data then
	      for _, cName in pairs(data) do
			if workspace.Creatures:FindFirstChild(cName) then
			  local newval = Instance.new("BoolValue")
			  newval.Name = cName
				newval.Parent = folder
					print("data2")
				else 
				print("no data2")
				player.savedata.Value = false
				player:Kick("Your data failed to load ! Please rejoin.")
	      end	
	   end
	end
end)

Well first off, you have to be able to delete player data because it’s an obligation under data protection laws. So you have to be able to delete all player records, and not just from your data stores.
Here is more information: About GDPR and CCPA

For data versioning, look under versioning here: Data Stores
But you could also do it a couple other ways. It’s really up to you

And yeah, your kick seems fine to me.

1 Like

i add this line in playerAdded it seems to work :

local success, errorMsg = pcall(function()
		while task.wait() do
			wait(180)
			saveData(player)
			print("autosave")
		end
	end)
end)

One problem I still have is this. I understand that it is related to the fact that the server is overloaded with “GetAsync” or “SetAsync” requests (I forget), however I don’t know how to fix this problem with my script. Thank you very much


Everytime i leave the game i have this warning message

It’s because of the request limits on datastores. They scale to the amount of players in your game, but in studio because you playtest often, you’re hitting the limits. There is a way to check what your current request limit budget is though here: DataStoreService:GetRequestBudgetForRequestType

Thank you again for the time you take to me! however i’m pretty sur there is something wrong who create this warning message everytime (every every).
Anyway, do you know where i need to place the
“1. A retry method in case the datastore fails to retry getting the data” ?
On playerAdded ? PlayerRemoved ? BindToclose ? On the local function Data Save ? Everywhere ?

On the playerAdded. If your datastore getAsync returns false, then retry maybe 3 times. If it’s false all 3 times then kick the player.

1 Like

Hey, Do you think line 87 ‘wait(“7”)’ can help to prevent from the warning message, or it is a very bad idea to do this ?
Capture

Edit : I think it is a bad idea since ok data4 and ok data3 dont show in the output when i leave

1 A retry method in case the datastore fails to retry getting the data → it’s OK
2 A way to kick a player if the datastores fail, so they don’t override/lose previous data → it’s OK
3 Data store history to revert to last save version → if someone can help me to achieve this please. I want to publish my game :sob:

i guess of putting game.Players each time u could use local Players = game:GetService('Players')

Yep, but does it make a difference in script ?

If anyone knows how to revert to the latest version of the data save a would be really great. Otherwise I won’t push it any further and will try to ask again in a few days. Thanks

no ur just making it shorter that way

1 Like