Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Multidimensional array is saying "attempt to index field '?' (a nil value)"

Asked by 6 years ago
Edited 6 years ago

I've tried to figure this out for a while now and just can't find the error. There is a comment above the line of code that triggers the error. Basically I'm trying to store the position data within a table (whenever the player clicks on certain parts) that can be quickly accessed for later use (I have no need to sort the data, and the data is only ever used for comparison purposes). Here are some code snippets that affect what I'm trying to do:

01local player = game:GetService("Players").LocalPlayer
02local equipped = false
03local depressStopped = false
04local part = ""
05local mouse = player:GetMouse()
06local distance = 0
07local tool = script.Parent
08local copy = ""
09local copyPosition = ""
10local copyPositionX = game.Workspace.Parttay.Position.X
11local copyPositionY = game.Workspace.Parttay.Position.Y
12local copyPositionZ = game.Workspace.Parttay.Position.Z
13local masterTable = {
14 
15[copyPositionX] = {[copyPositionY] = {[copyPositionZ]={copyPositionZ}}}
View all 28 lines...
01tool.Equipped:connect(function()
02    equipped = true
03    while equipped do
04        pcall(function()
05            distance = (mouse.Target.Position - game.Workspace[player.Name].Head.Position).magnitude
06            if mouse.Target:IsDescendantOf(game.Workspace.ConstantBlocks)  and distance < 20 then
07                part = mouse.Target        
08                position = part.Position
09            --other code
10            end
11        end)
12    end
13end)
14 
15tool.Unequipped:Connect(function()
View all 36 lines...

I'm still kinda new to this and would appreciate any pointers as well!

[EDIT]

I may just misunderstand how it works, but this code works just fine:

01part = game.Workspace.Parttay
02copyPositionX = part.Position.X
03copyPositionY = part.Position.Y
04copyPositionZ = 0
05print(copyPositionX, copyPositionY, copyPositionZ)
06local masterTable = {
07 
08[copyPositionX] = {[copyPositionY] = {[copyPositionZ] = {copyPositionZ}}}
09 
10}
11masterTable[copyPositionX][copyPositionY][copyPositionZ] = copyPositionZ
12print(masterTable[copyPositionX][copyPositionY][copyPositionZ])
0
First of all you should get rid of those dots in empty lines Tymberlejk 143 — 6y
0
That's to show there is more code, but I can separate the code snippets if it makes it easier. SteelMettle1 394 — 6y
0
I wasn't sure if this was a simple error on my part or not, so I didn't have every variable declared in the snippets. I just added all of them, I really am stumped on this though. SteelMettle1 394 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

In Lua, you can't declare a multidimensional array and then have it work for all combination of values, you have to make a new table for every index. That is, on line 15, you declare your masterTable, but you only initialize the tables needed for one coordinate.

If you wanted a 10x10x10 array with integer values, you could do this:

01local t = {}
02for x = 1, 10 do
03    t[x] = {}
04    for y = 1, 10 do
05        t[x][y] = {}
06        for z = 1, 10 do
07            t[x][y][z] = 0
08        end
09    end
10end

However, if you're wanting to put in arbitrary coordinates (ie they involve decimals or arbitrarily large integers), you'll want to use a function that sets up the tables on an as-needed basis (since you can't possibly make enough tables to cover all possible decimals):

01local masterTable = {}
02function store(x, y, z, value)
03    local tx = masterTable[x]
04    if not tx then
05        tx = {}
06        masterTable[x] = tx
07    end
08    local ty = tx[y]
09    if not ty then
10        ty = {}
11        tx[y] = ty
12    end
13    ty[z] = value
14end
15function get(x, y, z)
View all 33 lines...

I noticed you store a value identical to your 'z' coordinate - if there's nothing else being stored, that's redundant and you can remove the value (just store 'true' for example). The equivalent in a 1D table:

1local t = {}
2t[5] = 5 -- redundant
3t[5] = {5} -- redundant and harder to access the '5' at the end -- this is what you appear to be doing
4t[5] = true -- all you need
0
This will take me a bit to digest, but I think you may have solved it. The reason I added stored the key as the data is I needed it to compare, but with your method I could just compare the value in t[5] to true (since only those coordinates will allow it) SteelMettle1 394 — 6y
0
Thank you for your time/answer SteelMettle1 394 — 6y
Ad
Log in to vote
0
Answered by
clc02 553 Moderation Voter
6 years ago

For your error, since you didn't list a line it occurred on I can't tell you what line it happens on, but you're attempting access a table[x][y] where [x] doesn't exist. This usually happens if you're assuming the array is going to be a 5x5 array so you try to access table[5][5] without first making table[1] through table[5] empty arrays first.

1function initialize2d(x, y, default)
2  local t = {}
3  for i = 1, x do
4    t[i] = {}
5    for j = 1, y do
6      t[i][j] = default
7    end
8  end
9end

Answer this question