Data Store Function for Inventory?

Hi everyone I’m currently working on an Inventory System and rn I have a Problem that it doesn’t save the Item on Death(I need the Code for that pls and I enabled API Services here are 2 Scripts:

INVENTORY SCRIPT:
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)

INVENTORY LOCAL SCRIPT:

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)

AND I GOT THIS SMALL SWORD EQUIP SCRIPT THAT TELLS WHERE THE ITEMS SHOULD GO:

local promt = script.Parent.ProximityPrompt
local sword = script.Parent.Parent

promt.Triggered:Connect(function(player)
promt:Destroy()
sword.Parent = player.Inventory
end)

1 Like

“please do not ask people to design entire systems for you”

I’m guessing you want to save the tools they have?
You’ll need to get that.

local players = game:GetService("Players")
local store = game:GetService("DataStoreService"):GetDataStore("something")

players.PlayerRemoving:Connect(function(player:Player)
    local tools = {}
    for _, tool in ipairs(player.Inventory:GetChildren()) do
        table.insert(tools, tool.Name)
    end

    local success, result = pcall(store.UpdateAsync, store, player.UserId, function(old) return tools end)
    --etc.
end)

players.PlayerAdded:Connect(function(player:Player)
    local success, data = pcall(store.GetAsync, store, player.UserId)
    --etc.
end)
1 Like