Having problem with indexing a table

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:

you see, I need the frames to be ordered according to their layoutorder

it wouldn’t, because I’m overwriting the existing indexes. there are only 30 itemframes and each of them have a layoutorder number from 1-30, that’s their slot number.

You can also order their layouts based on their index.

I could, but what difference would that make? I’d have to clear the table, plus, my tables are just fine, not [“1”], [“2”] etc as you mentioned before

I thought your tables were broken

no, as you can see in the original post, I can index it just fine using
itemFrames[7], but not when 7 is in a variable

Something else I would like you to check is if the index numbers are actually the same. You never know, maybe the numbers are floating points and they aren’t actually equal, or maybe they are actually strings.

When you do the check here, also add:

print(index==7, type(index))
print(index, itemFrames[index], itemFrames[7])

Maybe it doesn’t cast numbers to strings if they’re variables. That’d explain why it would work with 7, a number which it’d cast to a string, but not with a variable.

ah, there you have it, the variable turns into a string (why would the index of a for loop be a string, though???)

image

It should be a number, no? How is the inventory table created? Did you use a string for the index there?

nope, as seen in the screenshots, the indexes of the inventory table are all numbers…

Quite literally what I was saying, lol.

it’s not, the index variable was a string, not the table. it had nothing at all to do with the tables.

What he’s trying to say is the inventory table might be using strings instead of numbers for the index. So when you iterate inventory, the variable index is a string.