Creating & Saving dictionary to a datastore

So I’m working on an inventory system, when a player joins the game, there is folders created as such:
7422cbe63f2f94702ed95f3e28ee8798

So what I wanted to do was actually way easier thought than done, I figured that when saving player data, I would loop through these folders (i would use values for ‘items’ and ‘materials’ etc.) and create a dictionary for the players stuff, the code I did for this was REALLY messy and confusing, so, look at it at your own risk.

game.Players.PlayerRemoving:Connect(function(player)
	local toSave = {}
	local Equipped = toSave.Equipped
	local Materials = toSave.Materials
	local Items = toSave.Items
	local Inventory = player.Inventory
	for _,v in pairs(Inventory.Equipped:GetChildren()) do
		local item = toSave.Items[v.Value]
	end
	for _,v in pairs (Inventory.Materials:GetChildren()) do		
		local item = toSave.Materials[v.Value]
		local quan = v.Quantity
		local quantity = toSave.Materials[v].quan
	end
	for _,v in pairs (Inventory.Items:GetChildren()) do
		local item = toSave.Items[v.Value]
	end
	local s, e = pcall(function()
		InventoryDS:SetAsync(player.UserId, toSave)
	end)
	
	if s then
		print("Success!")
	end
	
	if e then
		print("Error!")
	end
end)

I know there’s a lot of things that you can critique here, eg: the method of saving, using the userid as a key, so on and so forth, I’m aware of this and I just typed this all up to try and get something going at least.

This is what an item would of looked like in the explorer:
9d28f23ad81426f1a9b283ecfbd9659a

(string val name, int val quantity)

Does this code work or is it not saving?

It doesn’t work, here’s the error that occured when I tried to create some test values to save.

	for _,v in pairs(Inventory.Equipped:GetChildren()) do
		local item = toSave.Items[v.Value] -- the line that the error occured on
	end

error:

ServerScriptService.Inventory:38: attempt to index nil with ‘itemtest’

this is the error that I received ^
‘itemtest’ was the value of the “item”, aka the name of what the item would’ve been

Is it out of bounds with the array?

I’m not too sure what you’re asking, could you clarify?

Is the index you’re trying to reference in the array within the length of the array?

I was trying to add this item to the dictionary, I’m guessing that I’m doing it wrong, I’m not too sure how I can add that item to the dictionary, I’m aiming for the dictionary to look something along the lines of

	local toSave = {
		["Materials"] = 
			{
				["Wood"] = {
					["Quantity"] = 50
				},
				
				["Ore"] = {
					["Quantity"] = 30
				}
			},
		["Items"] = {
			"Item1",
			"item2"
		}
	}

It might be due to the way you are trying to access the data in the dictionary, by using v.Value. Have you tried using numbers?

v.Value was my attempt at saving the value of the integer / string values stored in the player’s inventory folder. At this point I don’t know how to save value into the dictionary.

Try and see if this works.

3 Likes

Might I ask, how would you read from the dictionary then? Supposing I :GetAsync() and retrieve the players data, how could I access for example the wood inside materials with quantity 50?

If you need to read from the already existing data to simply increment it use :UpdateAsync() instead of SetAsync.