Some of my players have been experiencing data loss when getting disconnected or teleporting between places from my experience.
-
What do you want to achieve?
Make the datastore more secure and avoid data loss at all costs. -
What is the issue?
There are players who are experiencing data loss. It specially only happens with players who got bad connection. They get kicked out of the game and their data is reseted. Sometimes they also teleport between places and their data doesn’t save at all. -
What solutions have you tried so far?
I tried looking for help or tutorials but I still don’t understand what’s the problem or what I could do.
This is my code
local datastore = game:GetService("DataStoreService"):GetDataStore("GameStore4 Test.1")
local backupLocation = game:GetService("ServerScriptService"):WaitForChild("BackupData")
local function SaveData(player)
local items = {player.Gamedatastats.OutfitMain.Value,player.Gamedatastats.Coins.Value,player.Gamedatastats.Inventory.Value,player.Gamedatastats.Fits.Value,player.Gamedatastats.Tickets.Value}
local key = "user-" .. player.userId
datastore:SetAsync(key, items)
local backupData = Instance.new("StringValue", backupLocation)
backupData.Name = key
backupData.Value = game:GetService("HttpService"):JSONEncode(items)
return items
end
game.Players.PlayerAdded:connect(function(player)
local Gamedatastats = Instance.new("IntValue")
Gamedatastats.Name = "Gamedatastats"
Gamedatastats.Parent = player
local OutfitMain = Instance.new("StringValue")
OutfitMain.Name = "OutfitMain"
OutfitMain.Parent = Gamedatastats
local Coins = Instance.new("NumberValue")
Coins.Name = "Coins"
Coins.Parent = Gamedatastats
local Inventory = Instance.new("StringValue")
Inventory.Name = "Inventory"
Inventory.Parent = Gamedatastats
local Fits = Instance.new("StringValue")
Fits.Name = "Fits"
Fits.Parent = Gamedatastats
local Tickets = Instance.new("NumberValue")
Tickets.Name = "Tickets"
Tickets.Parent = Gamedatastats
local key = "user-" .. player.userId
local storeditems = datastore:GetAsync(key)
if storeditems~=nil then
OutfitMain.Value = storeditems[1]
Coins.Value = storeditems[2]
Inventory.Value = storeditems[3]
Fits.Value = storeditems[4]
Tickets.Value=storeditems[5]
else
OutfitMain.Value = "Firstload"
Coins.Value = "350"
Inventory.Value = ""
Fits.Value = ""
Tickets.Value="0"
end
game:GetService("Players").PlayerRemoving:Connect(function(Player)
SaveData(Player)
end)
end)
game:BindToClose(function()
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
SaveData(player)
end
end)
local function SaveBackupData()
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
local key = "user-" .. player.userId
local items = {player.Gamedatastats.OutfitMain.Value,player.Gamedatastats.Coins.Value,player.Gamedatastats.Inventory.Value,player.Gamedatastats.Fits.Value,player.Gamedatastats.Tickets.Value}
local backupData = backupLocation:FindFirstChild(key)
if not backupData then
backupData = Instance.new("StringValue")
backupData.Name = key
backupData.Parent = backupLocation
end
backupData.Value = game:GetService("HttpService"):JSONEncode(items)
end
end
while wait(1000) do
SaveBackupData()
end