2D table Looping fail..

I have a 2D table that is in sh_gangs (included in the client file) and im trying to search for a gang thru it

for num,info in pairs(GANGS) do
if (string.find(num,SearchText:GetValue())) then

[@autorun\cl_gang.lua:91] bad argument #1 to ‘pairs’ (table expected, got nil)

The code for the table looks similar to this

GANGS["Gang Name"] = {["public"] = 1, ["money"] = 50000}

Similar to that.

Make sure you declare
[lua]GANGS = {}[/lua]

that is in sh_gangs (included in the client file)
that is in sh_gangs

Please read before posting

Or perhaps even tho I include the shared file, I have to redeclare it?

GANGS isn’t declared…

It is, In another file that is included in the client file.

Is it declared as local? Is it declared BEFORE you include the client file?

no, global.

I also ran a test that will print either Yes if its a nil, and no if its not a nil value.
It turns out not to be one, so I have no idea what the hell pairs is shitting about :o


I really dont understand why this is happening, I use a Lua Command Line for testing and experimenting, I’ve done the same scenario

GANGS = {}
GANGS["a"] = {val1=5,val2=8}
GANGS["b"] = {val1=2,val2=12}
for v,k in pairs(GANGS) do
if (v == "b") then

This is a roughly coded version of it (I wrote it as an example while I was typing, excuse any syntax errors)
Anyway: The Command Line gave no errors, and both of the pairs of prints (the one using k and GANGS[v]) returned the same value.

What is wrong?

I’ve just Copy+Pasted this into the command line, results were as expected.

If pairs is saying it’s nil, it is nil when pairs is being called.

Post a copy of the file where GANGS is declared.

GANGS = {}
local function Init()
local result
if (sql.TableExists("gang_info")) then
result = sql.Query("SELECT * FROM gang_info")
for num,info in pairs(result) do
GANGS[info["name"]] = {["ownerid"] = info["ownerid"],["public"] = info["public"],["money"] = info["money"]}


if (sql.TableExists("gang_player")) then
result = sql.Query("CREATE TABLE gang_player(steamid varchar(255), gang varchar(255))")
if (!result) then print("[[[SQL]]] Table Creation gang_player failed.") end
result = sql.Query("INSERT INTO gang_player VALUES('STEAM_0:1:26104897','Black Market')")
if (!result) then print("[[[SQL]]] Table Insertion Failed") end

GANGS["Black Market"] = {["public"] = 1, ["money"] = 50000}

Do keep in mind: The SQL stuff Is temporarily, I’m working on this with a friend of mine that will do the SQL stuff. This is just for testing. Anyway, this is included in the client and serverside file

Im gonna run some tests.

I see what I was doing wrong: I was looking at the console instead of my client, this is a clientside file.
Test Results: The tables are nil, but only when run on clientside?
The part of the code that runs on server too, returns that their not nil? What the hell?

Are you using AddCSLuaFile serverside and then including it from an already loaded clientside file? (like cl_init.lua)

Fixed: I didn’t AddCSLuaFile on the shared file