i made a system that creates a table and each time the backpack changes, it puts the name of all the items in their backpack into the table. when the player leaves, this table gets saved into a datastore. when the player rejoins, it will get the names of every item in the table and clone them into the backpack. however, ive noticed that its really inconsistent, and sometimes the backpack will return an empty table when the player leaves, even if the player actually had stuff in their inventory
#saving inventory on leave
1 messages · Page 1 of 1 (latest)
** You are now Level 4! **
show code
Alr
this is ragebait
local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerData")
local Players = game:GetService("Players")
local LeaderstatsModule = require(game.ReplicatedStorage.LeaderstatsModule)
local PlayerInventories = {} -- table of all the players in the server and their inventories
local function TrackInventory(player)
local PlayerInventory = {}
local function UpdateInventory()
table.clear(PlayerInventory) -- clear PlayerInventory to prevent duping
-- check items in backpacks
local Backpack = player:WaitForChild("Backpack")
for i, item in ipairs(Backpack:GetChildren()) do
if item:IsA("Tool") then
table.insert(PlayerInventory, item.Name)
end
end
--check items in character
local Character = player.CharacterAdded:Wait()
for i, item in ipairs(Character:GetChildren()) do
if Character then
if item:IsA("Tool") then
table.insert(PlayerInventory, item.Name)
end
end
end
end
-- set up listeners in the backpack and character
local Backpack = player:WaitForChild("Backpack")
Backpack.ChildAdded:Connect(UpdateInventory)
Backpack.ChildRemoved:Connect(UpdateInventory)
player.CharacterAdded:Connect(function(Character)
Character.ChildAdded:Connect(UpdateInventory)
Character.ChildRemoved:Connect(UpdateInventory)
end)
UpdateInventory()
return PlayerInventory
end
Players.PlayerAdded:Connect(function(player)
PlayerInventories[player.UserId] = TrackInventory(player)
print(PlayerInventories)
LeaderstatsModule.CreateLeaderstats(player, {"Points", "Rebirths"})
task.wait(0.5)
local PlayerData -- will assign a value to this variable through a pcall
local success, error_message = pcall(function()
PlayerData = PlayerDataStore:GetAsync(player.UserId)
end)
if success then
print(PlayerData)
print("Successfully loaded data for " .. player.Name .. ", " .. player.UserId)
end
if not success then
print("Error: " .. error_message)
end
player.leaderstats.Points.Value = PlayerData["Points"]
player.leaderstats.Rebirths.Value = PlayerData["Rebirths"]
for i, item in ipairs(PlayerData.Inventory) do
local ClonedItem = game.ReplicatedStorage.Items:WaitForChild(item):Clone()
ClonedItem.Parent = player.Backpack
end
end)
Players.PlayerRemoving:Connect(function(player)
local leaderstats = player:FindFirstChild("leaderstats")
local PlayerData = {
Points = leaderstats.Points.Value,
Rebirths = leaderstats.Rebirths.Value,
Inventory = PlayerInventories[player.UserId]
}
local success, error_message = pcall(function()
PlayerDataStore:SetAsync(player.UserId, PlayerData)
end)
if success then
print(PlayerDataStore:GetAsync(player.UserId))
end
if not success then
print("Error: " .. error_message)
end
end)
i was doing smth sry
ok so the bug apparently was that if the player didnt equip the item after loading in, it wouldnt get put in the table
so i just made it that the tool is parented to the character instead of the backpack when they spawn so that they are forced to equip it i guess
but then they have to unequip it for it to register 
just parent it back to the backpack after the item has been put in the table maybe?
And i would recomend using :UpdateAsync().
:SetAsync() seems to be not that reliable as :UpdateAsync() when it comes to data saving.
i found an alternative method that works i guess
for i, item in ipairs(PlayerData.Inventory) do
local ClonedItem = game.ReplicatedStorage.Items:WaitForChild(item):Clone()
table.insert(ClonedTable, ClonedItem.Name)
ClonedItem.Parent = player.Backpack
end
for i, item in ipairs(ClonedTable) do --[[put each item name in ClonedTable into the player's PlayerInventory table to make sure their items
are properly tracked when they spawn in
]]
table.insert(PlayerInventories[player.UserId], item)
end
i just added this inside the player added segment
alr
smart
wat does this mean 😭
i was using set async since i knew what to put as arguments
Let me make an example
alr ty
```lua
local NewValue
DataStore:UpdateAsync(Key, function()
return NewValue
)
The second Argument of UpdateAsync is just a function. And the value of that function is the new value that the data will be set to.
local success, error_message = pcall(function()
PlayerDataStore:UpdateAsync(player.UserId, function()
return PlayerData
end)
end)
i did this and i guess it worked?
ye
thanks
yeah
No problem. Good luck making your game