How would you find an Instance in a table?

Code that passes the table:

local inventory = script.Parent
local hotbar = inventory.Hotbar

local inventoryInfo = require(inventory.InventoryOptions)

local localPlayer = game.Players.LocalPlayer
local backPack = localPlayer:WaitForChild("Backpack")

function backpackLoop()
	local slotAmount = inventoryInfo.getSlotAmount(localPlayer)
	for i = 1, slotAmount do
		local newSlot = inventoryInfo.createSlot(i)
		inventoryInfo.formatSlot(newSlot, backPack[tonumber(i)].TextureId, backPack[tonumber(i)].Name)
		newSlot.Parent = hotbar
	end
end

localPlayer.CharacterAdded:Wait()
backpackLoop()

And the getSlotAmount() and formatSlot in the module:

invInfo.formatSlot = function(slot,image,name)
	if slot then
		if image then
			slot:FindFirstChild("MainLabel").Image = image
		else
			if name then
				slot:FindFirstChild("BackupLabel").Text = name
			else
				error("No Image or Name data found!")
			end
		end
	else
		error("No slot specified!")
	end
end

invInfo.getSlotAmount = function(localPlayer)
	if localPlayer then
		return #localPlayer.Backpack:GetChildren()
	else
		error("No localPlayer found!")
	end
end

The error is 1 is not a valid member of Backpack "Players.Vercte2.Backpack" at Client - InventoryManager:14

It gives this error because you used tonumber instead of tostring.

1 Like

In addition to what @RoBoPoJu said, if you want to look for the Instance with the numerical index
change the backPack variable’s value to localPlayer:WaitForChild("Backpack"):GetChildren()

Then from there, you can use the loop as normal

1 Like

But in this case, I don’t want the number/string “1”. I want to index it so I can find the first item in the Backpack. For example, in this case, say I want “abc” as the one chosen as it’s the first one in the array.