Having problem with indexing a table

so I can index a table like this:

  local item = table[7]

the item returns as normal.

but when I try doing it like this:

  local index = 7
  local item = table[index]

then item is nil… why could this be?

3 Likes

show us the whole script my man

the script is exactly the same as the example I gave

Context is important, we have no idea if the table has a metatable that acts differently or if your index variable is used incorrectly. Showing us everything is the only way to hopefully solve your problem here

for index, item in pairs(inventory) do
		local itemFrame = itemFrames[index]
		
		print(index, itemFrames[index], itemFrames[7])

Alright, I verified that everything is the same as in the example should be the same

here is what prints out:

7 nil ItemFrame

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.