How do I save an entire inventory?

Simple question. How would I save an entire inventory, including which items are equipped/unequipped. I’ve done this before, but let me tell you, it was NOT EFFICIENT AT ALL. I’m looking for an efficient, optimized way to do it. Anyone know how to help?

1 Like

Here’s a simple code that will save all of the tool names in to a table.

local DataStoreService = game:GetService("DataStoreService")

local ToolsDataStore = DataStoreService:GetDataStore("Tools")

local function SaveTools(Player)
	local ToolsTable = {}
	
	local Backpack = Player.Backpack
	local Character = Player.Character
	
	local StringUserId = tostring(Player.UserId) 
	
	for Index, Tool in ipairs(Backpack:GetChildren()) do -- Add backpack tools to the table
		if Tool["ClassName"] == "Tool" then
			table.insert(ToolsTable, Tool["Name"])
		end
	end
	
	local EquippedTool = Character:FindFirstChildOfClass("Tool")
	
	if EquippedTool then -- Add Equipped tool to the table
		table.insert(ToolsTable, EquippedTool["Name"])
	end
	
	local SaveSuccess = false
	
	repeat
		SaveSuccess = pcall(function() ToolsDataStore:SetAsync(StringUserId, ToolsTable) end) -- Save the table to the datastore
	until SaveSuccess -- repeat until save is successful (in case roblox servers are laggy)
	
	print("Successfully saved player's tools!")
end
3 Likes