Data Loss Issue

Some of my players have been experiencing data loss when getting disconnected or teleporting between places from my experience.

  1. What do you want to achieve?
    Make the datastore more secure and avoid data loss at all costs.

  2. 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.

  3. 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
2 Likes

Before you teleport the player:

  1. Save the data (and then make them have to load for like 15 ish seconds so that the datastorage doesnt get used too much)
  2. Send the data through the teleporting service (not suggested, because they bring the info with them)

Not sure what this is the cause of, you could else save their data every 5 minutes or so.

Perhaps ProfileService may be a viable solution for you. I experienced this same issue in my game when players would teleport or rejoin rapidly into servers and ever since I migrated to ProfileService, no such report has been made. :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.