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:
01 | local player = game:GetService( "Players" ).LocalPlayer |
02 | local equipped = false |
03 | local depressStopped = false |
04 | local part = "" |
05 | local mouse = player:GetMouse() |
06 | local distance = 0 |
07 | local tool = script.Parent |
08 | local copy = "" |
09 | local copyPosition = "" |
10 | local copyPositionX = game.Workspace.Parttay.Position.X |
11 | local copyPositionY = game.Workspace.Parttay.Position.Y |
12 | local copyPositionZ = game.Workspace.Parttay.Position.Z |
13 | local masterTable = { |
14 |
15 | [ copyPositionX ] = { [ copyPositionY ] = { [ copyPositionZ ] = { copyPositionZ } } } |
01 | tool.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 |
13 | end ) |
14 |
15 | tool.Unequipped:Connect( function () |
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:
01 | part = game.Workspace.Parttay |
02 | copyPositionX = part.Position.X |
03 | copyPositionY = part.Position.Y |
04 | copyPositionZ = 0 |
05 | print (copyPositionX, copyPositionY, copyPositionZ) |
06 | local masterTable = { |
07 |
08 | [ copyPositionX ] = { [ copyPositionY ] = { [ copyPositionZ ] = { copyPositionZ } } } |
09 |
10 | } |
11 | masterTable [ copyPositionX ] [ copyPositionY ] [ copyPositionZ ] = copyPositionZ |
12 | print (masterTable [ copyPositionX ] [ copyPositionY ] [ copyPositionZ ] ) |
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:
01 | local t = { } |
02 | for 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 |
10 | end |
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):
01 | local masterTable = { } |
02 | function 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 |
14 | end |
15 | function get(x, y, z) |
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:
1 | local t = { } |
2 | t [ 5 ] = 5 -- redundant |
3 | t [ 5 ] = { 5 } -- redundant and harder to access the '5' at the end -- this is what you appear to be doing |
4 | t [ 5 ] = true -- all you need |
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.
1 | function initialize 2 d(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 |
9 | end |