Add Data Store function so its saves my Inventory

Hi everyone I’m currently working on an Inventory System and rn I need that it saves my Inventory automatically when I leave the game and join again and idk how to do that I did tell me if I have to show something else than these scripts im gonna show

´´´
–InventoryScript:

local inventoryEvent = game.ReplicatedStorage.Remotes.InventoryEvent

game.Players.PlayerAdded:Connect(function(player)

local inventory = player:WaitForChild("Inventory")

local inventoryFrame = player.PlayerGui:WaitForChild("InventoryGui").InventoryFrame.ItemsFrame:GetChildren()

inventory.ChildAdded:Connect(function(Item)
	inventoryEvent:FireClient(player, Item.Name, true)
end)

end)

inventoryEvent.OnServerEvent:Connect(function(player, ItemName, Value, button)

 if Value == false then
		local SelectedItem = player.Inventory:FindFirstChild(ItemName)
		local backpack = player.Backpack:GetChildren()
		local stuff = player.Character:GetChildren()
		
		if #backpack == 0 and not player.Character:FindFirstChildWhichIsA("Tool") then
			button.Text = "Unequip"
			button.BackgroundColor3 = Color3.new(255,0,0)
			SelectedItem:Clone().Parent = player.Backpack
		else
			for i,v in ipairs(backpack) do
				button.Text = "Equip"
				button.BackgroundColor3 = Color3.new(0,255,0)
				v:Destroy()
			end
			for i, v in ipairs(stuff) do
				if v:IsA("Tool") then
					button.Text = "Equip"
					button.BackgroundColor3 = Color3.new(0,255,0)
					v:Destroy()
				end
			end
		end
 end

end)

´´´
–InventoryLocalScript

local InventoryEvent = game.ReplicatedStorage.Remotes.InventoryEvent
local itemFrame = script.Parent:FindFirstChild(“ItemsFrame”)

InventoryEvent.OnClientEvent:Connect(function(ItemName, Value)
if Value == true then

local ItemButton = script.Parent.ItemsFrame.ItemButton:Clone()
ItemButton.Visible = true
ItemButton.Name = ItemName
ItemButton.Text = ItemName
ItemButton.Parent = itemFrame

local equipButton = script.Parent.EquipFrame["EquipButton"]

	ItemButton.MouseButton1Click:Connect(function()
		script.Parent.EquipFrame.Title.Text = ItemName
		script.Parent.EquipFrame.Title.Visible = true
		equipButton.Visible = true
	end)

end
end)

1 Like

So I’m guessing you inventory is a heirachy of folders and not a table stored on the server?

If that’s the case you’re going to want too convert those folders into a table and then save that table

1 Like

how do I do this or how can I see if my inventory is an a table or nah u see im a completely noob

Here’s an article explaining how Data saving works and such - DataStores - Beginners to Advanced

And based off how I read your system, I would just save a table of the Item names, which I would then load by looping throught it.

1 Like