(I use ProfileService
)
Around once a week, sometimes player’s inventory data is reset, and I can’t figure out why. I am saving a lot of other similar data in the same way, and it has never been reset before. I’ll put each step that my data loading and saving goes through and hopefully someone can point out where it’s going wrong.
Here is where the data is loaded through a server Script
via PlayerAdded
. profile.Data.Items
is a blank table when the player first ever joins the game by default.
local function PlayerAdded(player: Player)
local hasLoaded = Instance.new("BoolValue")
hasLoaded.Name = "hasLoaded"
hasLoaded.Value = false
hasLoaded.Parent = player
local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId.."_Save5")
hasLoaded.Value = true
-- Cut out some unrelated code here
if player:IsDescendantOf(Players) == true then
Manager.Profiles[player] = profile
CreateDataFolder(player)
task.wait(3) -- I put a small delay due to weird inventory behavior if it loaded before my other client data. I do this with most of my data that are tables.
dataEvent:FireClient(player, "loadItems", profile.Data.Items)
else
profile:Release()
end
end
Here is where the client listens to that event and loads the items (within a ModuleScript
that is called by a LocalScript
after the hasLoaded
variable from above is set to true):
local ItemInfo = require(RS.Modules.ItemInfo)
local itemsLoaded = false --My functions that add/remove items return if this is still false.
local items = {}
function BackpackClient.Start()
dataEvent.OnClientEvent:Connect(function(eventtype, data)
if eventtype == "loadItems" then
BackpackClient.LoadItems(data)
end
end)
end
function BackpackClient.LoadItems(data)
for itemName, item: Types.Item in pairs(ItemInfo.Items) do
for i, loadedItem: Types.Item in pairs(data) do
if item.ItemId == loadedItem.ItemId then
item.ItemAmount = loadedItem.ItemAmount -- I load data this way so if I make changes to data held within an item, it will update for existing items.
table.insert(items, item)
end
end
end
itemsLoaded = true
BackpackClient.SetupBackpack()
end
I save the data for Items
when the player gets an item and after they use an item.
function BackpackClient.AddItem(itemName)
if itemsLoaded == false then return end
for i, item in pairs(items) do
local nameForSearch = string.gsub(item.Name, " ", "")
if nameForSearch == itemName then
item.ItemAmount = item.ItemAmount + 1
clientDataEvent:Fire("saveData", "Items", items)
return
end
end
-- This is for if the item isn't already in the inventory, then it makes an icon for that item, but I cut out that part of the code.
for i, item: Types.Item in pairs(ItemInfo.Items) do
if i == itemName then
item.ItemAmount = item.ItemAmount + 1
table.insert(items, item)
end
end
clientDataEvent:Fire("saveData", "Items", items)
end
function BackpackClient.RemoveItem(item)
if itemsLoaded == false then return end
for i, itemInfo in pairs(items) do
if itemInfo.Name == item.Name then
itemInfo.ItemAmount -= 1
if itemInfo.ItemAmount <= 0 then
table.remove(items, i)
end
clientDataEvent:Fire("saveData", "Items", items)
end
end
end
That event sends the info along to the server to be saved here:
function DataManagerServer.SaveData(player: Player, dataType, value)
local profile = Manager.Profiles[player]
local profileData = profile.Data
local playerDataFolderChildren = player.Data:GetChildren()
if dataType == "Items" then
profileData.Items = value
end
-- I cut out a lot of other data variables
for i, data in pairs(playerDataFolderChildren) do
if dataType == "Items" then continue end
-- I exempt tables from this because you can't store them within the player like you can `NumberValues`, etc.
if data.Name == dataType then
data.Value = value
end
end
end
From there, ProfileService
handles the saving. I have other tables of info being saved and none of them have ever reset, and I’ve compared them with how I’m saving my Items
and can’t tell where the data loss could be occurring. (Sorry if this was a long post I tried to keep it as short as possible)