Reading and writing from .txt files for a list

I was in the works of a whitelist addon for GMod servers
I define the list of SteamIDs at the top of the Lua file
I have a DFrame with some stuff on it that shows the whitelist and allows you to add SteamIDs to and from the whitelist
But then I realized something, any changes made to the whitelist from ingame would not save past a map change or a server restart do to the fact that the list of SteamIDs is redefined every time the file is ran

So I thought about how I could get it to save past a map change/server restart and I thought I could have a .txt file in my data folder where I could keep the SteamIDs and read and write from there so that the list could save
However, this would be the first time I’d be working with data files via Lua and I wasn’t sure how to go about it

I managed to get this much:

if (file.Exists("whitelist.txt", "DATA")) then

The required whitelist.txt data file does not exist and some functions of the whitelist addon will not work.

	local whitelist = {
		"STEAM_0:1:234567" //Example SteamID

Unfortunately, I’m not sure how I would define my whitelist and keep it in the data file (whitelist.txt)
I hope someone here will be able to explain this to me, thanks

input the table of steam ids into this function and then input the returned value into the second argument of

to receive the data from txt file, use file.Read and then util.JSONToTable the contents and there you go, a table of the steam ids

Alright, I got a file to be written containg the table, then I joined the server to test it out and I got an error each time I opened the whitelist window

[ERROR] addons/whitelistbyzee/lua/autorun/shared.lua:87: bad argument #1 to 'pairs' (table expected, got nil)
  1. pairs - [C]:-1
   2. func - addons/whitelistbyzee/lua/autorun/shared.lua:87
    3. unknown - lua/includes/modules/net.lua:32

Line 87 is the line that says “for k, v in pairs…”

	local DL = vgui.Create("DListView", DF)
	DL:SetSize(150, 245)
	for k, v in pairs( whitelist ) do

This is how I defined the whitelist table at the top via the data file, which is what I think is causing the error, since line 87 wasn’t giving me any errors before I switched the table over to the data file

if (file.Exists("whitelist.txt", "DATA")) then
	local content = file.Read("whitelist.txt", "DATA")
	local whitelist = util.JSONToTable(content)

Not sure what I’m doing wrong with that last section of code there
Any idea?

A few issues,
You probably have whitelist.txt saved on server, and you are trying to read it from the client,
also, you are making the whitelist local and you have no variable whitelist if the file doesn’t exist.

Make sure the file is on the client, though, I assume you are making a custom whitelist, so you are probs gonna have the whitelist saved to server, than send the whitelist to the user requesting it, through the net library*. I suggest you change the if statement to:

local whitelist = {}
if (file.Exists("whitelist.txt", "DATA")) then
	local content = file.Read("whitelist.txt", "DATA")
        whitelist = util.JSONToTable(content)

*[sp] Send the JSON string through net, not the table, than rebuild it clientside. It’ll be easier to send a string rather than a table. [/sp]