Attempt to Index nil with number in my Inventory System

So I have an inventory system but there’s a problem. Whenever I press the equip button, it prints out this:

Error Line
	slotF.Image = itemData.Items[1].TextureId
"Full Code
--Player
local player = game.Players.LocalPlayer
local backpack = player.Backpack
local char = player.Character or player.CharacterAdded:Wait()

-- Services
local TS = game:GetService("TweenService")
local SG = game:GetService("StarterGui")
local UIS = game:GetService("UserInputService")
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")

-- Disabling Backpack GUI
SG:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

-- Gui
local gui = script.Parent
local items = {}
local inventoryF = gui.Inventory; inventoryF.Visible = false
local hotbarF = gui.HotBar

local itemDescriptionF = inventoryF.ItemDescription
local itemNameT = itemDescriptionF:WaitForChild("Name")
local itemDescT = itemDescriptionF.Description
local itemImage = itemDescriptionF.ItemImage
local itemDmg = itemDescriptionF.Damage
local itemCooldwn = itemDescriptionF.Cooldown
local itemAbility = itemDescriptionF.Ability

-- Slots
local s1 = hotbarF.SlotF1.Slot1
local s2 = hotbarF.SlotF2.Slot2
local s3 = hotbarF.SlotF3.Slot3
local s4 = hotbarF.SlotF4.Slot4
local s5 = hotbarF.SlotF5.Slot5
local slots = {s1,s2,s3,s4,s5}

local hotbarItems = {}

local itemsSF = inventoryF.Items
local equipB = inventoryF.ItemDescription.EquipButton

local invSampleItem = script.Sample

local openB = script.Parent.Parent.ButtonsGui.Buttons.OpenButtonINV
local closeB = script.Parent.Inventory.CloseButtonF.CloseButton

openB.MouseButton1Click:Connect(function()
	inventoryF.Visible = not inventoryF.Visible
end)

closeB.MouseButton1Click:Connect(function()
	inventoryF.Visible = false
end)

-- Reference Variables
local selectedItem = nil

-- Scanning for Items
local function scanitems()
	
	-- Item List
	local items = {}
	
	-- Scanning Backpack
	for i,tool in pairs(backpack:GetChildren()) do
		if tool.ClassName == "Tool" then
			table.insert(items,tool)
		end
	end
	
	-- Scanning Character
	for i, tool in pairs(char:GetChildren()) do
		if tool.ClassName == "Tool" then
			table.insert(items, tool)
		end
	end
	
	-- Creating New List
	local invItems = {}
	
	for i, tool in pairs(items) do
		if invItems[tool.Name] then
			invItems[tool.Name].Count += 1
			table.insert(invItems[tool.Name].Items, tool)
		else
			invItems[tool.Name] = {
				Count = 1;
				Items = {tool};
			}
		end
	end
	
	return invItems
	
end

-- Updating Currently Selected Items
local function updateSelected(toSelectIB, toolData)

	-- Selecting New
	selectedItem = toolData.Items

	-- Changing Display
	itemNameT.Text = toolData.Items[1].Name
	itemDescT.Text = toolData.Items[1].ToolTip
	itemImage.Image = toolData.Items[1].TextureId
	itemDmg.Text = toolData.Items[1]:GetAttribute("Damage")
	itemAbility.Text = toolData.Items[1]:GetAttribute("Ability")
	itemCooldwn.Text = toolData.Items[1]:GetAttribute("Cooldown")
end

-- Removing Current Inventory Items
local function removeItems()
	for i, itemButton in pairs(itemsSF:GetChildren()) do
		if itemButton.ClassName == "ImageButton" and itemButton.Name ~= "Sample" then
			itemButton:Destroy()
		end
	end
end

-- Updating Display
local function updateDisplay()
	
	-- Clearing Out Current Inventory
	removeItems()
	
	-- Getting Inventory Tools
	local invItems = scanitems()
	
	for toolName, toolData in pairs(invItems) do
		
		-- Cloning Sample
		local itemButton = invSampleItem:Clone()
		itemButton.Parent = itemsSF
		itemButton.Name = toolData.Items[1].Name
		
		-- Setting Display
		itemButton.ItemName.Text = toolName
		itemButton.Image = toolData.Items[1].TextureId
		itemButton.Visible = true
		
		-- Connecting Buttons
		itemButton.MouseButton1Click:Connect(function()
			updateSelected(itemButton, toolData)
		end)
		
	end
	
	-- Hotbar Slots
	for i, slotF in pairs(slots) do
		slotF.Image = ""
	end
	for slotF, itemData in pairs(hotbarItems) do
		slotF.Image = itemData.Items[1].TextureId
	end
	
end

-- Loop
task.spawn(function()
	while true do
		task.wait(1)
		updateDisplay()
		
	end
end)

-- Equip Button
local equipButtonDB = false

local function equip()

	-- Choosing slot
	local chosenSlot = nil
	for i, slotF in pairs(slots) do
		if hotbarItems[slotF] == nil then
			chosenSlot = slotF
		end
	end

	if chosenSlot then

		-- Equipping to slot
		for slotF, itemData in pairs(hotbarItems) do
			if itemData.Items[1].Name == selectedItem.Items[1].Name then
				hotbarItems[slotF] = nil
			end
		end
		hotbarItems[chosenSlot] = selectedItem
		updateDisplay()

	end

end

-- Connecting Equip
equipB.MouseButton1Click:Connect(function()
	equip()
end)

The GUI hierarchy:

If you can help, it would be greatly appreciated! Thanks!

The problem could be that while loading in the value is not set yet, and the default is set to nil.

try doing

if itemData.Items ~= nil then
  slotF.Image = itemData.Items[1].TextureId
end```

You should combine your replies, instead of posting 2 replies

Okay so I tried that but it revealed another error:

The line:

if itemData.Items[1].Name == selectedItem.Items[1].Name then

Thank you for helping!

Send the other line of code that the error is occurring on

Do you mean the few lines after 188?

Yeah just the line the error happens on.

This is the only line in the conditional unless you mean something else

hotbarItems[slotF] = nil

Send a few of the lines before and after that so I can figure out why its happening.

	if chosenSlot then

		-- Equipping to slot
		for slotF, itemData in pairs(hotbarItems) do
			if itemData.Items[1].Name == selectedItem.Items[1].Name then
				hotbarItems[slotF] = nil
			end
		end
		hotbarItems[chosenSlot] = selectedItem
		updateDisplay()

	end

Try using this

hotbarItems = hotbarItems or {}
if chosenSlot and hotbarItems[chosenSlot] then
  hotbarItems[chosenSlot] = selectedItem
end

alright, where would I put it in the code? Would I just replace the lines with it?

Oh I didnt see that the value was nil on hotbarItems[slotF] = nil that is probably why its saying its indexing it with nil

Do I still use the code provided or not?

No, your issue is. You aren’t changing the value from nil, so when the code runs its detecting that error.

Do I change it to a number? Or do I do something else?

You have to change it to whatever value you are needing to work for your script. I haven’t read the whole script. But somewhere you are gonna need to change it from nil to some value to get the code to run without errors.

Alright, I’ll change it to 1 and I’ll let you know what happens. If everything works I’ll mark your latest post with code as a solution.

Alright let me know if it works. If you get an error lmk.