How to save items in an inventory?

So i made a datastore for an inventory where you can collect items but the there is an error that says this

What did i do wrong?

local ServerStorage = game:GetService("ServerStorage")
local InventoryTemplate = ServerStorage:WaitForChild("InventoryTemplate")
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

local function SetupInventory(player)
	local Inventory = player:WaitForChild("Inventory")
	local PlayerGui = player:WaitForChild("PlayerGui")
	local MainGui = PlayerGui:WaitForChild("MainGui2")
	local InventoryGui = MainGui:WaitForChild("InventoryGui")
	
	
	for i, item in pairs(Inventory:GetChildren()) do
		local itemGui = InventoryGui.Templates.Item:Clone()
		itemGui.Name = item.Name
		itemGui.ItemName.Text = item.Name
		itemGui.ItemQuantity.Text = item.Value
		if item.Value > 0 then
			itemGui.Visible = true
			itemGui.Parent = InventoryGui.ItemList
		else
			itemGui.Visible = false
			itemGui.Parent = InventoryGui.ItemList
		end
	end
end


game.Players.PlayerAdded:Connect(function(player)
	
	local GameData = InventoryTemplate:Clone()
	GameData.Name = "Inventory"
	GameData.Parent = player
	
	
	SetupInventory(player)
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-GameData")
	end)
	
	if success then
		GameData = data
	else
		print("There was an error getting your data")
		warn(errormessage)
		
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-GameData",player.Inventory)
	end)
	
	if success then
		print("Data Saved")
	else
		print("Error saving data")
		warn(errormessage)
	end
end)

You cannot store raw instances in datastores, so you would have to use serialization:

Source of error:

myDataStore:SetAsync(player.UserId.."-GameData",player.Inventory)

player.Inventory is an instance.

1 Like

the Serialization is kind of confusing for me

You could have a folder of every single tool in the game inside of ReplicatedStorage or something. Then, just save the names of all of the tools in their backpack. When they rejoin, just clone the tools by searching through the folder.

So i have every item in a folder in replicated storage already but how would i get that folder in replicated storage, and save it?

If you have the folder already, no need to make another. You could do some code like this for when they are leaving:

local savedItems = {}

for i,v in pairs(player.Backpack:GetChildren()) do
    if v:IsA("Tool") then
         table.insert(savedItems,v.Name)
    end
end

myDataStore:SetAsync(player.UserId.."-GameData",savedItems)

Then for the code when they are rejoining later could be something like:

local data
local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(player.UserId.."-GameData")
end)
	
local AllItemsInTheGame = game.ReplicatedStorage.Items:GetChildren()

if success then
	if data then
         for i,v in pairs(data) do
               if table.find(AllItemsInTheGame,v) then
                  local tool = AllItemsInTheGame[v]:Clone()
                  tool.Parent = player.Backpack
               end
         end
    end
else
	print("There was an error getting your data")
	warn(errormessage)
end

Just make sure there is a folder in ReplicatedStorage named Items, and it has all the tools. This is one way I would approach it, although I am not in studio right now so I have no clue if any of this code works. I think it should, but I am not that good with tables.

so this part is for player leaving?

local savedItems = {}

for i,v in pairs(player.Backpack:GetChildren()) do
    if v:IsA("Tool") then
         table.insert(savedItems,v.Name)
    end
end

myDataStore:SetAsync(player.UserId.."-GameData",savedItems)

And this is for when they Joining?

It is not. They would go in the other order. I misread it.

I didn’t include the PlayerAdded or PlayerRemoving functions. You need to put this code in those functions.

JOINING THE GAME:

local data
local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(player.UserId.."-GameData")
end)
	
local AllItemsInTheGame = game.ReplicatedStorage.Items:GetChildren()

if success then
	if data then
         for i,v in pairs(data) do
               if table.find(AllItemsInTheGame,v) then
                  local tool = AllItemsInTheGame[v]:Clone()
                  tool.Parent = player.Backpack
               end
         end
    end
else
	print("There was an error getting your data")
	warn(errormessage)
end

THIS IS FOR LEAVING THE GAME:

local savedItems = {}

for i,v in pairs(player.Backpack:GetChildren()) do
    if v:IsA("Tool") then
         table.insert(savedItems,v.Name)
    end
end

myDataStore:SetAsync(player.UserId.."-GameData",savedItems)
1 Like

The “Tools” are resources in the game like wood that you can collect in game, so i don’t think it should be added to the players backpack? Or is that needed?

Im not really sure how you made your inventory. Instead of looping through their backpack, search through whatever is in their custom inventory. Ive never made a custom inventory before, so I cannot really help you that much :confused:

About the part where you asked if you need to add it into your backpack, just add it back into the custom inventory. I assume it is a table.

instead of

tool.Parent = player.backpack

should i do

tool.Parent = InventoryGui.Itemlist

Because of this?
Screen Shot 2021-02-21 at 7.44.09 PM

Ok, I have no clue how you made your inventory system, but what do you put inside of ItemList? Gui Objects like Image Labels? Tools? Parts? Models?

You could just loop through the items and put them in the ItemList if it is their “inventory.”

The things inside the item list are like the objects like wood, stone and stuff

But what class are they?

For example:
ImageLabels
Parts
Models

I assume its image labels or frames because of the UIGridLayout.

yes images,frames,textlabels,textbutton

Alright then. Just have a folder with all the image labels and stuff in replicated storage. Use my code to save the names of all the image labels when they leave. Then when they rejoin, clone all the image labels by searching for them in the folder. Parent them to the player’s inventory gui or whatever and boom, its done!

Alright so i move that to rep storage?
Screen Shot 2021-02-22 at 4.08.50 PM