bad argument #1 to 'pairs' (table expected, got nil)

I have a lua file with a local table declared and defined at the top of the file. For some reason when I try to iterate through the table (in a function using) for k, v in pairs( requestedbans ) I get an error “bad argument #1 to ‘pairs’ (table expected, got nil)”. I have tried making the table global, but I get the same error. This confuses me because I have done the same thing previously and I was able to iterate through the table within a function.


local requestedbans = {}

function ulx.formatRequestedBan( ban )
  if type( ban ) ~= "table" or not ban.admin_name or not ban.ply_name or not ban.ply_steamid or not ban.reason or not ban.datetime then
    Msg( "Failed to format requested ban.
" )
    return
  end
  local formatted = "Admin: [green]" .. ban.admin_name .. "  [white]Player: [green]" 
  formatted = formatted .. ban.ply_name .. "[white](" .. ban.ply_steamid .. ")  Reason: "
  formatted = formatted .. ban.reason .. " [green]Time: " .. os.date( "%b %d, %Y - %I:%M:%S %p", ban.datetime ) .. "
"
  return formatted
end

function ulx.pullRequestedBans()
  requestedbans = util.JSONToTable( file.Read( "banrequests.txt", "DATA" ) )
end

function ulx.pushRequestedBans()
  file.Write( "banrequests.txt", util.TableToJSON( requestedbans ) )
end

function ulx.addRequestedBan( ban )
  if type( ban ) ~= "table" or not ban.admin_name or not ban.ply_name or not ban.ply_steamid or not ban.reason or not ban.datetime then
    Msg( "Failed to add requested ban.
" )
    return
  end
  ulx.pullRequestedBans()
  table.insert( requestedbans, ban )
  ulx.pushRequestedBans()
end

function ulx.removeRequestedBan( steamid )
  ulx.pullRequestedBans()
  for k, v in pairs(requestedbans) do
    if k.ply_steamid == steamid then requestedbans[k] = nil end
  end
  ulx.pushRequestedBans()
end

function ulx.printRequestedBans( ply )
  ulx.pullRequestedBans()
  local num = 0 --Number of requested bans
  for k, v in pairs(requestedbans) do
    num = num + 1
  end
  if num < 1 then
    return false
  else
    local i = num
    
    ply:PrintMessage( 3, "[green]There are bans that have been requested!"  )
    ply:PrintMessage( 3, "================================================="  )
    
    for k, v in pairs(requestedbans) do
      ply:PrintMessage( 3, ulx.formatRequestedBan( v ) )
      
      if i > 1 then
        ply:PrintMessage( 3, "----------------------------------------------------------------------------------------" )
      end
      
      i = i - 1
    end
    
    ply:PrintMessage( 3, "================================================="  )
    
    return true
  end
end

hook.Add( "PlayerInitialSpawn", "ulx.PlayerInitialSpawn.printRequestedBans", function( player )
  timer.Simple( 10, function() ulx.printRequestedBans( player ) end )
end)

Try passing the table into the function or setting it at the output e.g.


requestedbans = ulx.pullRequestedBans()

or


ulx.pullRequestedBans( requestedbans )

Does the file exist?

Using this code:


function pullRequestedBans( pullto )
  pullto = util.JSONToTable( file.Read( "banrequests.txt", "DATA" ) )
  for k, v in pairs( pullto ) do
    Msg( tostring(v) .. "
" )
  end
end

I get the same error as before, bad argument #1 to ‘pairs’ (table expected, got nil). This is when I call it at the bottom of the file with requestedbans as the argument (pullRequestedBans( requestedbans )).

Yes.

is the JSON string a valid string, which can be converted to a Valid table?

util.JSONToTable

If it failed to convert the JSON string into a table, it will return nil instead. Check and make sure it is valid by seeing if util.JSONToTable returns nil after an attempted conversion.

I didnt ask how to convert it, I asked him if hes sure that the string is not badly setup.

Try this replacement for your pull function; assuming your JSON file is formatted correctly.


function ulx.pullRequestedBans()
    requestedbans = {}
    table.CopyFromTo( util.JSONToTable( file.Read( "banrequests.txt", "DATA" ), requestedbans )
end

I’m not sure why this didn’t work, but I gave up and just made the functions use local tables.