Having problem with indexing a table

inventory and itemFrames are two different tables. Are you sure they are supposed to be used together like that?

yes, but that’s not the point of my post - I’m wondering why the first example works and why the second one doesn’t

Try printing out the entirety of both tables

I already have, I also verified that printing ItemFrames[7] works. Only when I try doing it with the index variable, it won’t work.

Show us a screenshot of what it looks like in the output. Also if you can, share the place file so we can test it ourselves

inventory table
image
itemframes table


print statement from code snippet
image

I already tested that the issue is not with any of the tables, I checked that the index was indeed 7, and that just using a plain number to index the table worked perfectly.

Can you share the place file with that script?

I’m afraid I’m not able to share that with you.

Then I’m afraid there’s no other way to help you, unless someone else comes and spots a problem that we all missed.

that’s alright, thanks for your time.

You can just make a baseplate and import all the bare minimum necessary scripts that are required for this problem and save that to a file. You don’t have to share the actual full game file with us, just the bare minimum in a baseplate causing the output you’re getting.

Why would I need to do that when I already isolated the issue? I verified that index is indeed a number (specifically, 7), that itemframes[index] is nil, and that itemframes[7] returns the expected value.

We don’t have the whole script bruh.

It’s for us to test it ourselves. Would be a huge waste of time if we constantly went back and forth trying to try and guess each problem individually. Like I said originally, we have no idea if the table has a metatable or if there’s something else in the script inteferring.

1 Like

this should be all of the relevant code

-- INVENTORY UPDATING

local itemFrames = {}

local function updateItemFrames()
	for _, itemFrame in pairs(Items:GetChildren()) do
		if not CollectionService:HasTag(itemFrame, "ItemFrame") then continue end
		
		itemFrames[itemFrame.LayoutOrder] = itemFrame
	end
	print(itemFrames)
end

local function updateInventory()
	updateItemFrames()
	
	for index, item in pairs(inventory) do
		local itemFrame = itemFrames[index]
		
		print(index, itemFrames[index], itemFrames[7])
		
		if itemFrame:FindFirstChild("Item") then
			itemFrame.Item:Destroy()
		end
		
		if not item or item == {} then continue end
		
		print(item["Name"])
		
		local Item = Instance.new("ImageLabel", itemFrame)
		Item.Name = "Item"
		Item.Size = UDim2.fromScale(0.7, 0.7)
		Item.Position = UDim2.fromScale(0.5, 0.5)
		Item.AnchorPoint = Vector2.new(0.5, 0.5)
		Item.BackgroundTransparency = 1
		Item.Image = "rbxassetid://" .. tostring( item.ImageId )
		
		local ItemAmount = Instance.new("TextLabel", Item)
		ItemAmount.Name = "ItemAmount"
		ItemAmount.Size = UDim2.fromScale(0.4, 0.3)
		ItemAmount.Position = UDim2.fromScale(1, 1)
		ItemAmount.AnchorPoint = Vector2.new(1, 1)
		ItemAmount.BackgroundTransparency = 1
		ItemAmount.TextScaled = true
		ItemAmount.TextColor3 = Color3.fromRGB(255, 255, 255)
		ItemAmount.Font = Enum.Font.GothamBlack
		ItemAmount.Text = tostring( item.Amount ) .. "x"
		
		local UIStroke = Instance.new("UIStroke", ItemAmount)
		UIStroke.Thickness = 3
		UIStroke.Transparency = 0.35
	end
end

You said this was it, we didn’t know :skull:

That’s because it returns on error after that point, because I’m trying to do :FindFirstChild() on nil.

btw if you want to know what it prints, check above for screenshots (:

itemFrames[itemFrame.LayoutOrder] = itemFrame

If itemFrames is an array, you should do table.insert() instead. I can see this being the issue; because one will result in a table like this:
{
[1]=?,
[2]=?,
}

The other like this
{
[“1”]=?,
[“2”]=?,
}

These are different tables.

In addition, something else I noticed is that itemFrames never gets reset/cleared when it is being updated. You should use table.clear() for that. Otherwise, there’s going to be artifacts in the table and even potentially memory leaks. Just another observation :stuck_out_tongue: