Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Saving folder's children with DataStore?

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?

1 answer

This is because you are overriding a key twice when saving. Think about it like delivering mail.

  1. You get the key to the whole entire mailbox
  2. You put mail in the mailboxes
  3. You lock the mailbox

If you save to the same box (or key), you cannot find it because it is buried with the new mail (the key was overwritten). To fix this, you can use tables like so

local data = {
	['money'] = 500;
	['rating'] = 10/10;
	['datastores working'] = true
}

So in your case, you'd do

game.Players.PlayerRemoving:connect(function(player)
    --wait(2) oh my gosh the player is gonna be gone by now
    local key = "tools_" .. tostring(player.UserId)
    local inv = player:FindFirstChild("Inventory"):GetChildren()
	table.insert(itemlist, player.leaderstats.cash.Value)

    DataStore:SetAsync(player.UserId, itemlist)
    DataStore:SetAsync(player.UserId, player.leaderstats.cash.Value)
end)

Also, I recommend keeping the Itemlist in ServerStorage because different players have different items, right?

Another tip: You should use :BindToClose() like so to extend the server length when no one is in da server

game:BindToClose(function()
	warn('D: why did everyone leave dis is not a bad game ')
	wait(30)
end)

Hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.