Attempt to index nil with number

Hello developers, I was just casually working on my game, and then I was working on an inventory gui. I was scripting it when I got an error I couldn’t fix.


So here’s the script (Line with error at the end of the post)

local tweenService = game:GetService("TweenService")
local starterGui = game:GetService("StarterGui")
local userInputService = game:GetService("UserInputService")
local debris = game:GetService("Debris")
local players = game:GetService("Players")

local player = players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
local character = player.Character or player.CharacterAdded:Wait()

starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)

local screenGui = script.Parent
local hotbarFrame = screenGui:WaitForChild("Hotbar"); hotbarFrame.Visible = true
local inventoryFrame = screenGui:WaitForChild("Inventory"); inventoryFrame.Visible = false

local itemDescriptionFrame = inventoryFrame:WaitForChild("ItemDescription")
local itemNameText = itemDescriptionFrame:WaitForChild("Name")
local itemDescriptionText = itemDescriptionFrame:WaitForChild("Description")

local equipNotification = inventoryFrame:WaitForChild("EquipNotification")

local s1 = hotbarFrame:WaitForChild("Slot1")
local s2 = hotbarFrame:WaitForChild("Slot2")
local s3 = hotbarFrame:WaitForChild("Slot3")
local s4 = hotbarFrame:WaitForChild("Slot4")
local s5 = hotbarFrame:WaitForChild("Slot5")
local s6 = hotbarFrame:WaitForChild("Slot6")
local s7 = hotbarFrame:WaitForChild("Slot7")
local s8 = hotbarFrame:WaitForChild("Slot8")
local s9 = hotbarFrame:WaitForChild("Slot9")
local s10 = hotbarFrame:WaitForChild("Slot10")
local slots = {s1,s2,s3,s4,s5,s6,s7,s8,s9,s10}

local hotbarItems = {}

local itemsScrollFrame = inventoryFrame:WaitForChild("Items")
local inventorySampleItem = itemsScrollFrame:WaitForChild("Sample")
local closeButton = inventoryFrame:WaitForChild("CloseButton")
local equipButton =  inventoryFrame:WaitForChild("EquipButton")

local selectedItem = nil

local openButton = screenGui:WaitForChild("OpenButton")

local openPosition = UDim2.new(0.5,0,0.5,0)
local closePosition = UDim2.new(0.5,0,-0.5,0)

inventoryFrame.Position = closePosition

local function openInventory()
	if inventoryFrame.Position ~= closePosition then
		return
	end
	local info = TweenInfo.new(
		1,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.Out,
		0,false,0
	)
	local props = {
		Position = openPosition;
	}
	local tween = tweenService:Create(inventoryFrame,info,props)
	
	inventoryFrame.Visible = true
	tween:Play()
end

local function closeInventory()
	if inventoryFrame.Position ~= openPosition then
		return
	end
	local info = TweenInfo.new(
		1,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.In,
		0,false,0
	)
	local props = {
		Position = closePosition;
	}
	local tween = tweenService:Create(inventoryFrame,info,props)
	
	tween:Play()
	tween.Completed:Wait()
	inventoryFrame.Visible = false
end

openButton.MouseButton1Up:Connect(function()
	if inventoryFrame.Position == openPosition then
		closeInventory()
	else
		openInventory()
	end
end)
closeButton.MouseButton1Up:Connect(closeInventory)

local function updateSelected(toSelectImageButton,toolData)	
	selectedItem = toolData.Items[1].Name
	
	itemNameText.Text = toSelectImageButton.Name
	itemNameText.Text = toolData.Items[1].Name
	itemDescriptionText.Text = toolData.Items[1].ToolTip
end

local function removeItems()
	for i,itemButton in pairs(itemsScrollFrame:GetChildren()) do
		if itemButton.ClassName == "ImageButton" and itemButton.Name ~= "Sample" then
			itemButton:Destroy()
		end
	end
end

local function scanItems()
	local items = {}

	for i,tool in pairs(backpack:GetChildren()) do
		if tool.ClassName == "Tool" then
			table.insert(items,tool)
		end
	end

	for i,tool in pairs(character:GetChildren()) do
		if tool.ClassName == "Tool" then
			table.insert(items,tool)
		end
	end

	local inventoryItems = {}

	for i,tool in pairs(items) do
		if inventoryItems[tool.Name] then
			inventoryItems[tool.Name].Count += 1
			table.insert(inventoryItems[tool.Name].Items,tool)
		else
			inventoryItems[tool.Name] = {
				Count = 1;
				Items = {tool};
			}
		end
	end
	return inventoryItems
end

local function updateDisplay()
	removeItems()

	local inventoryItems = scanItems()

	for toolName,toolData in pairs(inventoryItems) do
		local itemButton = inventorySampleItem:Clone()
		itemButton.Parent = itemsScrollFrame
		itemButton.Name = toolData.Items[1].Name

		itemButton:FindFirstChild("CountDisplay").Text = toolData.Count.."x"
		itemButton.Image = toolData.Items[1].TextureId
		itemButton.Visible = true

		itemButton.MouseButton1Up:Connect(function()
			updateSelected(itemButton,toolData)
		end)
	end
	
	for i,slot in pairs(slots) do
		slot.Image = ""
	end
	
	for slotFrame,itemData in pairs(hotbarItems) do
--
--
--
--
--
		slotFrame.Image = itemData.Items[1].TextureId -- line with error
--
--
--
--
--
	end
end

local equipButtonDebounce = false
local function equip()
	if selectedItem == nil or equipButtonDebounce == true then return end
	equipButtonDebounce = true	
	equipNotification.Visible = true
	
	local input,gameProcessedEvent = userInputService.InputBegan:Wait()
	
	local keyToSlots = {
		[Enum.KeyCode.One] = s1;
		[Enum.KeyCode.Two] = s2;
		[Enum.KeyCode.Three] = s3;
		[Enum.KeyCode.Four]= s4;
		[Enum.KeyCode.Five] = s5;
		[Enum.KeyCode.Six] = s6;
		[Enum.KeyCode.Seven] = s7;
		[Enum.KeyCode.Eight] = s8;
		[Enum.KeyCode.Nine] = s9;
		[Enum.KeyCode.Zero] = s10
	}

	local chosenSlot = nil
	for keyCode,slot in pairs(keyToSlots) do
		if input.KeyCode == keyCode then
			chosenSlot = slot
			break
		end
	end

	if chosenSlot == nil then
		local oldText = equipNotification.Text
		local oldColor3 = equipNotification.BackgroundColor3

		equipNotification.Text = "Invalid Key"
		equipNotification.BackgroundColor3 = Color3.new(0,0,0)
		equipNotification.Visible = true

		task.wait(1)
		
		equipNotification.Visible = false
		equipNotification.Text = oldText
		equipNotification.BackgroundColor3 = oldColor3
	else
		hotbarItems[chosenSlot] = selectedItem
		print(hotbarItems)
	end
	
	print(hotbarItems)
	equipButtonDebounce = false
end

equipButton.MouseButton1Up:Connect(equip)

task.spawn(function()
	while true do
		task.wait(0.1)
		updateDisplay()
	end
end)

Line with error:

slotFrame.Image = itemData.Items[1].TextureId

Thank you so much for helping me with this error.

itemData.Items seems to be nil in this case.

1 Like

I looked at where itemData was created but still didn’t find anything.

itemData seems to be the tool that was put into the hotbar. Could you do

print(itemData)

inside of the for loop before the error occurs?

1 Like

It just printed testTool2 or whatever tool I try to put into the slot.

Then I’m assuming you meant for the line to be like this:

slotFrame.Image = itemData.TextureId

I’ve tried that, and it gives me the error:
Unable to assiggn property Image. Content expected, got nil

Oh, then the problem is that the tool doesn’t have a defined TextureId.

if itemData.TextureId then
	slotFrame.Image = itemData.TextureId
end

The tool does have a TextureId.

That’s quite weird. Is the slotFrame an image label or an image button? or is the Image another instance?

print(itemData.TextureId)
slotFrame.Image = itemData.TextureId or ""

slotFrame is indeed, very much an image label.

Alright, could you try using that piece of code I have provided? It should allow me to get some insight as to why this problem is occuring.

1 Like

itemData.TextureId is nil it says, but there is a TextureId in the tool.

Ok, is the texture id not the property of the tool but rather of a whole different instance?

Could be of an image label. Im not sure but Im pretty sure itemData is a tool. Like I said Im not to sure. I’ll remember to organize my scripts a little better in the future.

In this case, I believe itemData is a tool as well, and assuming TextureId is the property, there should be no problems. Just try printing just case

print(itemData.ClassName)
slotFrame.Image = itemData.TextureId or ""

itemData is super indeed in fact nil it says.

I think I figured out the problem. The problem is that itemData is a Name and not a tool.

Change this to

selectedItem = toolData.Items[1]

And it should be like this:

if itemData.TextureId then
	slotFrame.Image = itemData.TextureId
end

Basically developers, make sure the instance your trying to do something with is the instance the function is made for.