Hello everyone,
Sometimes I get reports of players saying they have lost some of their data (like an item they bought in game). I can’t figure out why this happens and it frustrates me a lot.
I have to mention that I never shutdown any server before doing an update to the game. Could this be the cause of dataloss? Or is the way I am saving data the problem?
Below here is a brief example of how I save data. In this example I save Energy and the inventory of a player (this inventory gets filled by purchasing something in game, like a pet)
local dataStores = game:GetService("DataStoreService"):GetDataStore("BuckDataStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local totalplayers = 0
game.Players.PlayerAdded:Connect(function(player)
totalplayers = totalplayers + 1
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local energy = Instance.new("IntValue")
energy.Name = "Energy"
energy.Parent = leaderstats
local playerData = Instance.new("Folder")
playerData.Name = player.Name
playerData.Parent = game.ServerStorage.PlayerData
local inventory = Instance.new("Folder")
inventory.Name = "Inventory"
inventory.Parent = playerData
local energydata
local InventoryData
pcall(function()
energydata = dataStores:GetAsync(player.UserId.."-Energy")
end)
pcall(function()
InventoryData = dataStores:GetAsync(player.UserId.."-Inventory")
end)
if energydata then
energy.Value = energydata
end
if InventoryData then
for _, item in pairs(InventoryData) do
if game.ServerStorage.Items:FindFirstChild(item) then
local ItemClone = game.ServerStorage.Items[item]:Clone()
ItemClone.Parent = inventory
end
end
end
end)
local bindableEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
dataStores:SetAsync(player.UserId.."-Energy", player.leaderstats.Energy.Value)
end)
pcall (function()
local items = game.ServerStorage.PlayerData[player.Name].Inventory:GetChildren()
local itemstable = {}
for _, v in pairs(items) do
table.insert(itemstable,v.Name)
end
dataStores:SetAsync(player.UserId.."-Inventory",itemstable)
end)
totalplayers = totalplayers - 1
bindableEvent:Fire()
end)
game:BindToClose(function()
while totalplayers > 0 do
bindableEvent.Event:Wait()
end
end)
Does anyone know what the cause of people losing data could be?