I've made an inventory system, but I want to save it with DataStore2 and I have no clue where to start

You hover over the solution for your post (such as post 8) and it should be in the bottom corner near the reply button.

This “:Get()” , doesn’t seem to be working at all for me, any ideas why?

LOADING v

local Defaults = {}

function Added(plr)
	
	local Inventory = Instance.new("Folder")
	Inventory.Parent = plr
	Inventory.Name = "Inventory"
	
	local InventoryStore = DataStore2("Inventory", plr)
	

	InventoryStore:Get(Defaults)

	
local function UpdateUI(plr)
	for _, item in pairs(Inventory:GetChildren()) do	
		if item then
			local InventoryUI = plr.PlayerGui:WaitForChild("Inventory").Outer.Inventory
			local Template = InventoryUI.Template:WaitForChild("ItemImage")
			local itemclone = Template:Clone()
			itemclone.Visible = true
			itemclone.Parent = InventoryUI
			itemclone.Name = item.Name
			local Name = itemclone:FindFirstChild("ItemName")
			Name.Text = item.Name
		end
	end
end



UpdateUI(plr)

end



game.Players.PlayerAdded:Connect(Added)

SAVING V

	--> Variables
	
	local Inventory = plr:WaitForChild("Inventory")
	
	local Defaults = {}
	
	local InventoryStore = DataStore2("Inventory", plr)
	
	local InventoryTable = InventoryStore:GetTable(Defaults)

	
	for i,v in ipairs(Pickupables.Weapons) do
		if item and item.Name == v and (item.PrimaryPart.Position - humrp.Position).Magnitude <= 5 and item then
			if Inventory:FindFirstChild(item.Name) then
				
			elseif not Inventory:FindFirstChild(item.Name) then
				local Item = Instance.new("NumberValue")
				Item.Name = item.Name
				Item.Value = 0
				Item.Parent = Inventory
				table.insert(InventoryTable, Item.Name)
				table.insert(InventoryTable, Item.Value)
				InventoryStore:Set(InventoryTable)
				print("The player has just picked up a "..item.Name)
				item:Destroy()
			end
		end
	end
end)

Check your output, you need to create a boolvalue with a specific name in serverstorage and enable it to save in studio.

Also you are not hooking your datastore to :Update().

There is not error in the output, it is saving the data but not loading it at all

Yes, because you need to hook it up to :Update(). Don’t forget to also use :Set and :Increment to cause any change at all to your inventory. It should be → :Set() → :Update() → Inventory updating

It also says saved Inventory in the output so it’s saving, How would I update the data in my situation using :Update() ?

inventorystore:Update(newinventorytable)
     --get rid of the guis of items that are gone, add the guis that aren't currently present
end)

newinventorytable is literally just the new inventory table after a :Set()
so it would be

inventorystore:Set(newinventorytable)

and it would pass through with update

1 Like

Still very confused, why am I removing the guis of items when the player hasn’t dropped an item

You are removing the guis of the items that are no longer there.
Old table could be, {“Sword”, “Katana”}
New table could be {“Sword”, “Zweihander”}
So in update you would check to see if there are any gui elements that are no longer in the table (such as katana) and delete them, then see if sword and zweihander exist. Sword does, but zweihander does not, so you would add zweihander to the gui.

Hmmm, interesting could you show me an example of this also, I don’t have a Zweihander for my inventory system

Zweihander was just an example, and no I cannot provide an example as it would be writing code on your behalf. Developer forum is to ask and resolve problems, not ask people to make code for you. Again, this is for you to figure out.

Oh, I get it, i see, i see, well thanks for your time man

This is how I save inventory in my game.

Every player has a folder named “Inventory” in a folder ReplicatedStorage.PlayerData.PlayerData_USERID. When a player leaves, I run a for loop to iterate over every single item gotten by using :GetChildren(). Then I add names of those items and how many there are into a table that will then look like this:

local inventory = {
	"SomeItem1" = 1,
	"SomeItem2" = 6
}

That table is then converted to JSON with HttpService:JSONEncode() and saved to the datastore.

When a player joins the game, it obviously has to get the JSON from the datastore first. If it is nil which means the player is new then starter inventory will be cloned into the player data folder of that player. If it contains JSON, then it is converted to a table using HttpService:JSONDecode(). After that, a script iterates over that table and it tries to find an item from ServerScriptService.Items which is where I have copies of all available items in game. The script tries to find an item from there with :FindFirstChild(key) where key is the key of a key value pair of the decoded inventory table. If it finds an item, it will clone it (value) times into the player inventory folder. If it doesn’t find any item with that name (if it was removed in an update or something like that) then it ignores that key value pair.

Pretty simple and efficient for my game.

1 Like

What have you tried?
can we have your attempted implementation at least?


Although several posts made before this demonstrate possible implementations, here’s another

For an ‘inventory system’, assuming only objects under a player’s Backpack or a folder are to be saved, save the names of all items under a player’s backpack to the DataStore in an array and upon the player joining back in, search for items with the specified names in a folder preferably in ServerStorage and make all of them available to the player. There are several ways to do what you want to achieve.

Do you mean you convert tables to JSON manually? that’s done internally already when saving to a DataStore as only strings can be saved, JSONEncoding tables yourself just increases the amount of characters you’re saving and isn’t efficient.

Oh dude I got this down, I don’t need anymore help! Thanks!

The manual JSON conversion is just a piece of the old saving system I used for the old data saving system. I coded a better data saving system but the manual conversion stuck and I haven’t bothered to remove it.