How to make Data Store script more secure?

I am looking for a way to make my Data Store script more secure to prevent save data loss in my game. Its very rare but I am getting the occasional report from someone that they lost all their save data in my game. I am looking for ideas on how to change or improve the script.

The script:

local DataStoreService = game:GetService("DataStoreService")
local Players, DataStoreService = game:GetService("Players"), game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

local function load(player: Player)
	local playerUserId = "Player_"..player.UserId
	local data = nil
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)
	if success then
	else
		print("Save Error")
		warn(errormessage)
	end
end

local function save(player: Player)
	local playerUserId = "Player_"..player.UserId
	local data = {
	}


	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)

	end)

	if success then
		print("Save Success")
	else
		print("Save Error")
		warn(errormessage)
		myDataStore:SetAsync(playerUserId, data)
	end
end

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:wait()
	load(player)
end)

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

game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		coroutine.wrap(save)(player)
	end
end)

What do you mean exactly by secure?

You should consider using DataStore2 or ProfileService as there shouldn’t ever be data loss with them. I know DataStore2 has never had data loss according to the creator, profile service I don’t know much about but have heard that it’s also very good for data stores. Seems like personal preference between these two modules but I use DataStore2 and would recommend it

well, that’s why i don’t really use normal datastores, instead, i would rather Datastore 2, ikr its the same thing but ds2 is ‘‘more complete’’ and safe, since it generates backups and stuff, you can read more in the api, also, it’s way more easier to handle all data

your current code don’t need to be deleted, only adapted to Datastore 2 ( removing all datastore code of course)

don’t worry if this would be the case

By secure I mean players not losing data.

I’ll make sure to look into it

i really don’t know how data loss happens, but probaly has something like

‘’ server tried 10x to save but dind’t loaded or saved stuff at all’’

in some rare ocasions, i see the own CoreGui from roblox in ROBLOX studio not loading,
and when internet connections fail sometimes, also FAILS, that means, while playing, if somehow the player leaves at the same time they lost connection, idk what would happen,

for this, make sure to make a Extra save button, BUT this button saves as usual,

if you already have one, then ok, but add something like

copy all data, insert in a table each piece of data, then just save inside datastore

after that, you could make more backups like the method below:

some people say that you can put values inside a folder and just save the folder, in Serverstorage, then when loading, if any folder is found ( with the Player.ID), it will take it, and load it,

if somehow Folder 1 is missing, but folder 2 exists ( the backup of backup )
then he will take it , and this should repeat until X the loop in the folders end

probaly datastore 2 works like that, idk i never studied the whole stuff

ProfileService integrates with DataStore2 in a newer version.