Backpack not loading items

I’m trying to save the Player’s Backpack, it’s… working…? But at the same time, it’s not. When i leave the game, the Backpack save, and when i enter, it’s supposed to load, sometimes the Backpack load, sometimes not.

I already tried to make the Backpack load when the item is loaded, but it’s still not working, any idea?

Here’s the script, it can save and load any item, but the item need to be in the ServerStorage.

local DataStoreService = game:GetService("DataStoreService")
local SaveBackpack = DataStoreService:GetDataStore("Inventory")
local Items = game.ServerStorage

local function LoadInventory(Player)
	local success, data = pcall(function()
		return SaveBackpack:GetAsync(Player.UserId)
	end)
	if success then
		if not data then
			data = {"","","","","","","","","","","","","","","","","","","","","","","",""}
		end
		if typeof(data) ~= "table" then
			data = {data,data,data,data,data,data,data,data,data,data,data,data,data,data,data,data,data,data,data,data,data,data,data,data}
		end
		if data then
			for i = 1,24,1 do
				if Player:WaitForChild("Backpack") then
					if data[i] ~= "" and Items:FindFirstChild(data[i]) then
						local ItemClone = Items:FindFirstChild(data[i]):Clone()
						ItemClone.Parent = Player.Backpack
						print(ItemClone.Parent.Name.." | "..ItemClone.Name)
					end
				end
			end
			print(data)
		end
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	LoadInventory(Player)
end)

local function SaveInventory(Player)
	local InventoryToSave = {"","","","","","","","","","","","","","","","","","","","","","","",}
	local Backpack = Player.Backpack:GetChildren()
	for i = 1,24,1 do
		if Backpack[i] then
			InventoryToSave[i] = Backpack[i].Name
		else
			InventoryToSave[i] = ""
		end
	end
	local success, err = pcall(function()
		SaveBackpack:SetAsync(Player.UserId, InventoryToSave)
		print(InventoryToSave)
	end)
end

game.Players.PlayerRemoving:Connect(function(Player)
	SaveInventory(Player)
end)

There’s some “Print()” to test, but all are working properly. And is there anything that can be optimized?

By the way, the Backpack have a number max of items, it can store up to 24 items, but it’s already working.

Is there any error in the output?

Ok, that is horrible to look at. Using 24 data entries manually in itself is a pain. Workarounds for that:

local data = {}
for i = 1, 24, 1 do
    data[i] = ""
end

--or

setmetatable(data, {["__index"] = function(self, index)
    return ""
end}

Anyway, I think the issue is the game doesn’t have enough time to save data when the player leaves
which can be solved with a BindToClose.

--even something as simple as a wait statement at the end can solve this, you can make it save player data if you need
game:BindToClose(function()
    task.wait(5)
end)

Some other key points I would recommend implementing:

  • Data versioning and UpdateAsync
  • Data budgeting
  • Retry logic when saving

Also, your connection at the bottom can just be simplified to:
game:GetService("Players").PlayerRemoving:Connect(SaveInventory)

Are you testing in studio or in game because if you are on studio it doesn’t save

That’s not true. That’s the issue where data does not save because the server closes before it has time to save it, which can be fixed with a BindToClose.

Yeah that’s what I meant to say I just forgot to add the bind thing sorry

1 Like

Thank you for the first part, but i don’t think that this is the problem, because of the Print(), i know it’s saving when the game close and loading when the game opens, but the items is not appearing in the Backpack.

And no, There’s nothing but what should be there.

I Fixed the problem. Basically, when i pick a item, it go to the backpack, when i leave the game, the script save what’s in the Backpack, but sometimes when i enter the game, the items go to the StarterGear instead of the Backpack, so it didn’t show in my Backpack Gui, and since it’s wasn’t in the Backpack, it didn’t save. To solve this, i made the script create 2 Clones, one for the Backpack and the other to the StarterGear, and this is working now.