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!
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)
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
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 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