• Print

Author Topic: Reading and writing from .txt files for a table  (Read 7079 times)

0 Members and 1 Guest are viewing this topic.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Reading and writing from .txt files for a table
« on: March 09, 2015, 08:18:48 pm »
Was working on the final and hardest part (for me) of my whitelist addon when I kept getting this same error
Code: [Select]
[ERROR] addons/whitelistbyzee/lua/autorun/shared.lua:86: bad argument #1 to 'pairs' (table expected, got nil)
  1. pairs - [C]:-1
   2. func - addons/whitelistbyzee/lua/autorun/shared.lua:86
    3. unknown - lua/includes/modules/net.lua:32

Line 86 is the one that reads "for k, v in pairs..."
Code: Lua
  1.         local DL = vgui.Create("DListView", DF)
  2.         DL:SetMultiSelect(false)
  3.         DL:SetPos(5,50)
  4.         DL:SetSize(150, 245)
  5.         DL:AddColumn("SteamIDs")
  6.         for k, v in pairs( whitelist ) do
  7.                 DL:AddLine(v)
  8.         end

However, I don't believe the issue is there, for before I switched my table over to a data file, that line gave no errors

Instead of the "whitelist" table being defined at the top of the Lua file like I had before, I decided it would be best for me to keep it in a data file named whitelist.txt due to the fact that if I didn't, the idea of editing the list from ingame would be useless since it would reset with every map change/restart

That being said, this is now the code at the top of the file

Code: Lua
  1. local uwhitelist = {
  2.         "STEAM_0:1:7099", //Garry
  3.         "STEAM_0:0:55581053" //The creator of this addon
  4. }
  5. local uaj = util.TableToJSON(uwhitelist) //uaj stands for Uwhitelist As JSON
  6. local cwf = file.Read("whitelist.txt", "DATA") //cwf stands for Current Whitelist File
  7. if (uaj==cwf) then
  8.         local whitelist = util.JSONToTable(cwf)
  9. end

util.JSONToTable is supposed to return a table, so I'm not sure why it's not working


PS:
When I commented out the if statement on line 7 and 9 (but leaving line 8 valid), I got a different error that I still don't understand, since file.Read returns a string and whitelist.txt is definitely not empty
Code: [Select]
[ERROR] addons/whitelistbyzee/lua/autorun/shared.lua:8: bad argument #1 to 'JSONToTable' (string expected, got no value)
  1. JSONToTable - [C]:-1
   2. unknown - addons/whitelistbyzee/lua/autorun/shared.lua:8

Incase you want to know, this is the current state of whitelist.txt
Code: [Select]
{"1":"STEAM_0:1:7099","2":"STEAM_0:0:55581053"}

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: Reading and writing from .txt files for a table
« Reply #1 on: March 09, 2015, 09:43:46 pm »
I may be wrong because I'm a bit rusty at Lua...  If you declare a variable inside of an if statement, I believe it gets localized to that statement?  So it gets tossed out after the statement has ended.

try

Code: [Select]
local whitelist = {}
if (uaj==cwf) then
whitelist = util.JSONToTable(cwf)
end

EDIT:  I cannot spot the error with the other bit, but here is what I use that is similar if you can maybe spot something.

Code: [Select]
if file.Exists( "ulx/restrictions.txt", "DATA" ) then URS.restrictions = util.JSONToTable( file.Read( "ulx/restrictions.txt", "DATA" ) ) end
« Last Edit: March 09, 2015, 09:48:55 pm by Aaron113 »

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Reading and writing from .txt files for a table
« Reply #2 on: March 10, 2015, 07:49:18 am »
Have you written the file clientside? If you don't have the file on the clientside, then it (obviously) can't read it.
I haven't done any GLua in a long time, but if I remember right - the recommended way to send data to (or from) the server to the client, check out the net library.
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Reading and writing from .txt files for a table
« Reply #3 on: March 10, 2015, 09:18:33 am »
It's a shared file (garrysmod/lua/autorun/shared.lua) and I define the whitelist outside of the (CLIENT) and (SERVER) if statements
Is that an issue? I figured if I didn't make the table realm specific I wouldn't have to use the net library to send it back and forth

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: Reading and writing from .txt files for a table
« Reply #4 on: March 10, 2015, 10:38:04 am »
Is the error client side or server side?

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: Reading and writing from .txt files for a table
« Reply #5 on: March 10, 2015, 11:27:50 am »
I did something pretty much like this:
/index.php/topic,6255

So you may be able to get some ideas from it, as it's pretty much the idea on how you want.
« Last Edit: March 10, 2015, 11:35:46 am by Bite That Apple »
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Reading and writing from .txt files for a table
« Reply #6 on: March 10, 2015, 11:56:09 am »
Is the error client side or server side?

I think the error is clientside
In the server console, it says "Zee... Lua Error" just above the error

I did something pretty much like this:
/index.php/topic,6255

So you may be able to get some ideas from it, as it's pretty much the idea on how you want.
Oh didn't even notice that, I'll check it out, thanks

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Reading and writing from .txt files for a table
« Reply #7 on: March 10, 2015, 12:22:23 pm »
I decided to move the file reading, table defining, and json/table conversion stuff to inside of my (SERVER) if statement and then just send over the table via a net message when the client needs it and for whatever reason, that worked

Incase anyone's wondering, this is now the top of my the Lua file
Code: Lua
  1. if (SERVER) then
  2.  
  3. local allowedranks = { //These are the ranks allowed to add, remove, and view SteamIDs on the whitelist from ingame. This is compatible with ULX ranks
  4.         "superadmin",
  5.         "admin",
  6.         "owner"
  7. }
  8. local uwhitelist = {
  9.         "STEAM_0:1:7099", //Garry
  10.         "STEAM_0:0:55581053" //The creator of this addon
  11. }
  12. local uaj = util.TableToJSON(uwhitelist) //uaj stands for Uwhitelist As JSON
  13. local cwf = file.Read("whitelist.txt", "DATA") //cwf stands for Current Whitelist File
  14. local whitelist = util.JSONToTable(cwf)

Thanks for all of the help, guys
I'm getting closer and closer to finishing now and I couldn't have done it without the help of you

  • Print