I am trying to save a players whole inventory folder with 1 save. For example, when a player joins he will start with a sword, and by the time he leaves he could have multiple swords in his inventory, and it will save on leave.
Code:
local DataStore = game:GetService("DataStoreService"):GetDataStore("Test")
local Players = game:GetService("Players")
local itemlist = {}
game.Players.PlayerAdded:connect(function(player)
local key = "tools_" .. tostring(player.UserId)
-- inv
if not player:FindFirstChild("Inventory") then
local invs = Instance.new("Folder",player)
invs.Name = "Inventory"
end
wait()
local inv = player:WaitForChild("Inventory")
-- starting sword
if not inv:FindFirstChild("LinkedSword") then
local ls = Instance.new("IntValue",inv)
ls.Name = "LinkedSword"
ls.Value = 1
table.insert(itemlist, ls.Name)
end
DataStore:SetAsync(player.UserId, itemlist)
wait()
-- leaderstats
local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"
local cash = Instance.new("IntValue",leader)
cash.Name = "Cash"
cash.Value = DataStore:GetAsync(player.UserId) or 0
cash.Changed:Connect(function()
DataStore:SetAsync(player.UserId, cash.Value)
end)
end)
game.Players.PlayerRemoving:connect(function(player)
wait(2)
local key = "tools_" .. tostring(player.UserId)
local inv = player:FindFirstChild("Inventory"):GetChildren()
DataStore:SetAsync(player.UserId, itemlist)
DataStore:SetAsync(player.UserId, player.leaderstats.cash.Value)
end)
The leaderstats is a test that does save, but saving the itemlist table breaks it, making it not save at all. Whenever i remove the itemlist table save, the playerstats will save, but otherwise, no. I am trying to make it where if a player gets another weapon in his inventory, it will put it into the table and save it. Any help?
