Table of table as argument = nil

Hey, I am trying to create a sub_table and pass the parent_table as argument to a server event. But inside the event function any access to sub_table returns nil, parent_table works fine though.

What is the proper way do this in LUA?

Client side:

local parent_table = {}
local sub_table = {}

sub_table["element1"] = 'One'
sub_table["element2"] = 'Two'
parent_table["number"] = sub_table

TriggerServerEvent('usefull:event', parent_table)

Server side:

RegisterServerEvent('usefull:event')
AddEventHandler('useful:event', function(table)
    print(type(table.number.element1))
end)

The server side will print: nil

Please post the code of the server handling this event:

TriggerServerEvent('usefull:event', parent_table)

Just edited it.

What does the following do:

RegisterServerEvent('usefull:event')
AddEventHandler('useful:event', function(parent_table)
    print(parent_table[0])
end)

Note that I have changed the variable name, since table is used for other purposes as well (i.e. table.insert).

This should return something like table <address garbage>

It prints nil

Oh sorry, I meant print(parent_table["number"])

It does prints: table: 0x7f135…
Which means the parent_table is passing through the event, right?

Then it’s getting some sort of data… I think you can access the sub table now? :thinking:

local sub_table = parent_table["number"];
print(sub_table["element1"])
print(sub_table[0])
print(sub_table[0]["element1"])

Idk, one of these must work… I guess the serialization over the network makes table.number.element1 be no longer valid…

That printed:

nil
nil
Error attempt to index nil value

I guess serialization then just punted that sub table… weird?..

I was thinking that since LUA will pass things as reference, the reference to sub_table is left behind at the client side. Anyway, supose I should just create one shallow huge table and move on.

Found the problem, I was trying to serialize a vector3. Switched for lua types and it works now.

1 Like