I have attempted to make a system to save the inventory of someone(the tools in their backpack) and load them in when they re-join, but it isn't working or returning any errors. Nothing is being printed to the inventory.
--Sorry my code is a bit messy
local serverStorage = game:GetService("ServerStorage")
local dss = game:GetService("DataStoreService")
local inventoryStore = dss:GetDataStore("Inventory")
game.Players.PlayerAdded:connect(function(plr)
local key = "user_"..plr.userId
print(inventoryStore:GetAsync(key))
if inventoryStore:GetAsync(key) then do
repeat wait() until plr:FindFirstChild("Backpack")
for i,v in ipairs(inventoryStore:GetAsync(key)) do
if v.Name == "MP5" then do
serverStorage:WaitForChild("MP5"):clone().Parent = plr.Backpack
print("loading mp5")
end else if v.Name == "Police Baton" then do
serverStorage:WaitForChild("Police Baton"):clone().Parent = plr.Backpack
print("loading baton")
end
end
end end end end
end)
game.Players.PlayerRemoving:connect(function(plr)
local key = "user_"..plr.userId
local saveInventory = {}
for _,item in pairs(plr.Backpack:GetChildren()) do
table.insert(saveInventory,1,item.Name)
end
inventoryStore:SetAsync(key, saveInventory)
for _,item in (saveInventory) do
print(item.Name)
end
end)

1 comment
I have also tried to print the contents of saveInventory but nothing prints