Trying to find a table inside a table

I am working on a game that uses sprite sheets, and I want to use tables to refer the direction and state of the sprite, but I cannot find a way to refer to a table inside a table
Here is a little of the code that makes the table, then another one that reads the table

-local FramesIdle = {
	down = {Vector2.new(-64, 0), Vector2.new(-32, -1), Vector2.new(0, 1)},
	up = {Vector2.new(-64, 32), Vector2.new(-32, 31), Vector2.new(0, 33)},
	left = {},
	right = {}

} 
--Table that refers to the up, down, left and right part of the sprite sheet I use for the idle state
while true do
	if moving --[[This is a bool]] == false then
		for i = 1, 3 do --There is only three frames inside the animation
			script.Parent.ImageRectOffset = framesidle[direction[i]] -- This exact line is where my problem is
			wait(0.15)
		end				
	end
end

Is my coding skills bad? Should I do something else? Did I miss the obvious? Tell me in the comments!

2 Likes

In the second section you write framesidle but in the first section you write FramesIdle, have you double checked your capitalisation?

1 Like

You can do it by doing this on line 4:

framesidle[direction][i]

2 Likes

I capitalized it only to show it here, but everything in the actual script is actually functional.

Are you getting an error when running?

Yes, but it is only related to the line where I try to reference the table

Did you change it to this?

The way it is in the script doesn’t correctly reference the current frame

I get an error.
attempt to index nil with number
I think it does not detect the framesidle[direction][i] since there aren’t any tables associated with the key [i]

framesidle[direction] references the direction in the table (down, up, left, right) framesidle[direction][i] would reference the frame of that direction (down 1, down 2, down 3)

Exactly, but I cannot reference the frame number according to the direction

Give me a minute, i’m gonna run the code and see what happens

Where / what do you define direction?

Direction is defined by the latest input the player has done, Pressing D will make the player move down in one second, making the character turn downwards after moving

This code successfully accessed the Vector2 value

local FramesIdle = {
	down = {Vector2.new(-64, 0), Vector2.new(-32, -1), Vector2.new(0, 1)},
	up = {Vector2.new(-64, 32), Vector2.new(-32, 31), Vector2.new(0, 33)},
	left = {},
	right = {}

} 

local moving = false
local direction = 'down'

while true do
	if moving --[[This is a bool]] == false then
		for i = 1, 3 do --There is only three frames inside the animation
			script.Parent.ImageRectOffset = FramesIdle[direction][i] -- This exact line is where my problem is
			wait(0.15)
		end				
	end
end
local table1 = {
}
local table2 = {
}
local tableTable = {
 table1;
 table2;
}

this worked for me when i had a similair problem.

1 Like

FramesIdle[direction][i]
This part in specific runs error attempt to index nil with number and there is nothing I can do

That’s kind of weird, code works fine for me

This worked, now I am going to add this to the notes for myself!

local FramesIdle = {
	down = {Vector2.new(-64, 0), Vector2.new(-32, -1), Vector2.new(0, 1)},
	up = {Vector2.new(-64, 32), Vector2.new(-32, 31), Vector2.new(0, 33)},
	left = {},
	right = {}
} 

local Direction = "down"
local moving = false

while task.wait() do
	if not moving then
		for i = 1, 3 do
			script.Parent.ImageRectOffset = FramesIdle[Direction][i]
			task.wait(0.15)
		end				
	end
end

You pretty much already had it.

Yeah, this works, I’m guessing the original poster changed moving/direction accidentally.

1 Like