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.