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)