Inventory System Help

Hello! I am currently working on an inventory system. It works great, however I would like to save it. I looked through many posts on here and the most concrete answer was “putting items in a table and saving them”. So, I tried that. However, I am running into some trouble actually adding items to the table.

Here is my code.

Server
local InventoryItems = {}

local InventoryEvent = game.ReplicatedStorage.Events.InventoryEvent

game.Players.PlayerAdded:Connect(function(Player)
	local Inventory = Instance.new("Folder", Player)
	Inventory.Name = "Inventory"
	
	Player.Inventory.ChildAdded:Connect(function(Item)
		InventoryEvent:FireClient(Player, Item.Name, true)
		
		InventoryItems[Player][Item.Name] = InventoryItems[Player][Item.Name] + 1
	end)
	
	InventoryEvent.OnServerEvent:Connect(function(Player, ItemName, Value)
		if Value == false then
			local SelectedItem = Player.Inventory:FindFirstChild(ItemName)
			
			InventoryItems[Player][ItemName] = InventoryItems[Player][ItemName] - 1
			
			if SelectedItem:IsA("Model") then
				SelectedItem.Parent = game.Workspace.GroundItems
				SelectedItem:SetPrimaryPartCFrame(Player.Character.HumanoidRootPart.CFrame + Player.Character.HumanoidRootPart.CFrame.LookVector * 5)
			else
				SelectedItem.Parent = game.Workspace.GroundItems
				SelectedItem.CFrame = Player.Character.HumanoidRootPart.CFrame + Player.Character.HumanoidRootPart.CFrame.LookVector * 5
			end
		end
	end)
end)
Client
local InventoryEvent = game.ReplicatedStorage.Events:WaitForChild("InventoryEvent")
local ItemFrame = script.Parent:FindFirstChild("ItemFrame")

InventoryEvent.OnClientEvent:Connect(function(ItemName, Value)
	if Value == true then
		local ItemButton = ItemFrame:FindFirstChild("Sample"):Clone()
		ItemButton.Visible = true
		ItemButton.Name = ItemName
		ItemButton.Text = ItemName
		ItemButton.Parent = ItemFrame
		
		ItemButton.MouseButton1Click:Connect(function()
			ItemButton:Destroy()
			InventoryEvent:FireServer(ItemName, false)
		end)
	end
end)

Anyway, it errors on line 19 of my Server script which is

InventoryItems[Player][Item.Name] = InventoryItems[Player][Item.Name] + 1

And the error I get is

14:28:35.549  ServerScriptService.InventoryServer:19: attempt to index nil with 'Oak Log'  -  Server - InventoryServer:19
  14:28:35.550  Stack Begin  -  Studio
  14:28:35.550  Script 'ServerScriptService.InventoryServer', Line 19  -  Studio - InventoryServer:19
  14:28:35.551  Stack End

I’m not good with tables nor DataStores so please point me in the right direction. It would mean a lot! Thanks.

First of all, you can’t save objects in datastore, you can only save strings and tables and numbers, so what you wanna do is save the tool’s string and then try to find it in game and give the tool back when the name maches the tool’s name

1 Like

I recommend learning tables backwards and forwards. Get the table fully sorted out. Then when you go to save, save the whole table! Table first, then storage.

As for how to add items into the table, do not index based on the item name. Use a number index and a dictionary.

local tablesize = #inventory[player]
inventory[player][tablesize + 1] = {
Name = “Oak Log”,
Quantity = 2}

If you use the index [“Oak Log”] you cant have more than one stack.

1 Like