As the title says, I am having trouble saving the players inventory, and loading it. I am new to DataStores, and the most I can do is save some leaderstats and clothing.
I have been coding for about 5 hours now, working on my wizard game, but I still can’t figure this out. I tried looping through the players inventory when they leave, but my brain just fries up from there and I forget what I’m doing.
If someone could give me an explanation on how to do this, I would be very grateful.
Click me (;
Check out this sweet climbing mechanic I made while you’re at it
Oh my god.
My brain just exploded and I remembered how to do everything.
local DataStoreService = game:GetService("DataStoreService")
local playerStuffStore = DataStoreService:GetDataStore("MyDataStore")
local players = game.Players
players.PlayerAdded:Connect(function(player)
local inventoryData
local success, err = pcall(function()
inventoryData = playerStuffStore:GetAsync(player.UserId .. "-inventory")
end)
if success then
if inventoryData ~= nil then
for i = 1, #inventoryData do
local tool = game.ServerStorage.Tools:FindFirstChild(inventoryData[i])
if tool then
tool:Clone().Parent = player.Backpack
end
end
else
end
else
print("There was a problem loading "..player.Name.."'s inventory. ->")
warn(err)
end
end)
players.PlayerRemoving:Connect(function(player)
--loop through players inventory, and add tools to a table
local inventory = {}
for i, tool in pairs(player.Backpack:GetChildren()) do
table.insert(inventory,tool.Name)
end
local success, err = pcall(function()
playerStuffStore:SetAsync(player.UserId.."-inventory",inventory)
end)
if success then
print("Inventory successfully saved!")
else
print("There was an error saving data ->")
warn(err)
end
end)
Thank you sm, because you kinda woke me up. You can have the solution