Net Library and Tables

Hi there,
do you guys have any idea why I can’t get the net library working when I send this table?

Client:
[lua]
function LoadInv()
RunConsoleCommand( “getitems” )
net.Receive(“BatmanTable”, function(l)
tbl = net.ReadTable()
end)

for k, v in pairs(tbl) do
    print(v)
end

end
[/lua]

Server Concommand:
[lua]
concommand.Add(“getitems”, function( p, c, a )
p:GetItemsSV()
end)
[/lua]

Server Function:
[lua]
function pm:GetItemsSV( )
net.Start(“BatmanTable”)
net.WriteTable({“Batman”, “Superman”})
net.Send(Entity(1))
end
[/lua]

Not really sure, but I believe the “tbl” variable you define on line 4 of the supplied clientside code is available only in the net.Receive callback function. To make it available outside of it, it should be enough to define it first - haven’t tested, but I guess it could work.

[lua]
tbl = {}

function LoadInv()
RunConsoleCommand( “getitems” )
net.Receive(“BatmanTable”, function(l)
tbl = net.ReadTable()
end)

for k, v in pairs(tbl) do
    print(v)
end

end
[/lua]

Also, a note. The net.Receive function is enough to be called only once per each NetworkString (this is the ‘BatmanTable’ in your case), there’s no point in calling it every time you load the inventory. This code should do the same:

[lua]
function LoadInv()
RunConsoleCommand( “getitems” )
end

net.Receive(“BatmanTable”, function(l)
local tbl = net.ReadTable()

 for k, v in pairs(tbl) do
      print(v)
 end

end)
[/lua]

The problem is you declared the callback function where you expect to get it. Code does not execute like this. It will not simply just wait for the table to be sent to the client. instead of parsing through the table immediately move your for loop within the callback function as such.

[lua]
function LoadInv()
RunConsoleCommand( “getitems” )
end

net.Receive(“BatmanTable”, function(l)
tbl = net.ReadTable()
for k, v in pairs(tbl) do
print(v)
end
end)
[/lua]