Add Save function to Inventory when I leave?

Hi everyone I’m currently working on an goofy Inventory System I’m asking for this for the fifth time how can I save the InventoryFrame and everything when I leave, so the Inventory System saves when I die but not when I leave and join again so pls help somehow I got some scripts and if the Solution could be a basic and not difficult code I’d be so happy!TY

–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)

Sup, if you are looking to learn how to save and load data, feel free to check out this post DataStores - Beginners to Advanced as not so often is someone on DevForum willing to script entire sections of code for you.

Why not save to your each time you get an inventory item and remove an item?
This way you shouldn’t have to automatically save it when you leave because it’ll already be saved.

someone offered me this help before but um it was too complicated for me to be honest im not really that pro scripter

how do I do that do I have to code sth. for that?

You would have to code for that.

When a player acquires an item, save that addition to your datastores at that moment.
When a player drops or loses an item remove that item from their datastores inventory.

You may be able to get help from the Studio Assistant for your scripting. In the View tab select Assistant and type a specific request into it. It probably won’t give you an exact script, but it’ll contain the elements you’ll need to change in your script.

1 Like