saving data

alright so im using file.Write or whatever and im attempting to make a table inside it…

so when the player joins i hook it to a player join function to create a data file with there steamid64…

in the file on join the file has {} in it (to be a table…) then when they buy a car… (im making a car dealer) i have this function run



		if !(file.Exists("lmm_cardealer_data", "DATA")) then
			for k, v in pairs(player.GetAll()) do
				v:ChatPrint("Car Dealer files not found! Please restart the server!")
			end
			return
		else
			local currentfile file.Read( "lmm_cardealer_data/"..ply:SteamID64()..".txt", "DATA" )
			local addingfile = table.insert( currentfile, rcname )
 			file.Write( "lmm_cardealer_data/"..ply:SteamID64()..".txt", addingfile )
		end


the error i get is



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


Now no i did not expect for this to work but how would i go upon working this out…

I see 2 problems. (maybe 3)

  1. rcname I don’t see you define this.
  2. For storing tables in files you can use
    1. util.JSONToTable( <JSON> ) – for the data coming from a file
    2. util.TableToJSON( <TABLE> ) – for converting a table into json in order to save it to file.

So in order to get this to work you need to make sure that you convert your table to json when saving and your json to a table when reading.

The error you are getting because you are trying to insert into a string. (in this case you missed the = so lua says its nil.)

[editline]17th September 2015[/editline]

here is at least your error fixed.


		if !(file.Exists("lmm_cardealer_data", "DATA")) then
			for k, v in pairs(player.GetAll()) do
				v:ChatPrint("Car Dealer files not found! Please restart the server!")
			end
			return
		else
			local currentfile = util.JSONToTable( file.Read( "lmm_cardealer_data/"..ply:SteamID64()..".txt", "DATA" ) or "[]" ) or {}
			local addingfile = table.insert( currentfile, rcname )
 			file.Write( "lmm_cardealer_data/"..ply:SteamID64()..".txt", util.TableToJSON( addingfile ) )
		end


so this is what i did:



		if !(file.Exists("lmm_cardealer_data", "DATA")) then
			for k, v in pairs(player.GetAll()) do
				v:ChatPrint("Car Dealer files not found! Please restart the server!")
			end
			return
		else
			local currentfile = util.JSONToTable( file.Read( "lmm_cardealer_data/"..ply:SteamID64()..".txt", "DATA" ) or "[]" ) or {}
			local addingfile = table.insert( currentfile, rcname )
				file.Write( "lmm_cardealer_data/"..ply:SteamID64()..".txt", util.TableToJSON( addingfile ) )
		end


Now all that happones is in the data file it clears everything so the text goes from “” to “” (empty)

i never used json before so sorry if im a newb at this xD

[editline]17th September 2015[/editline]

also there are NO errors and rcname is defined in the config so no worries about that

Okay. that means you write “” into the file somewhere. Start debugging by adding print() on every line :slight_smile:

print( rcname )
or
PrintTable(currentfile)
PrintTable(addingfile)
(or both)

at the end of the if statement.

You made me laugh :freakout:

Now i did this:



		if !(file.Exists("lmm_cardealer_data", "DATA")) then
			for k, v in pairs(player.GetAll()) do
				v:ChatPrint("Car Dealer files not found! Please restart the server!")
			end
			return
		else
			local currentfile = util.JSONToTable( file.Read( "lmm_cardealer_data/"..ply:SteamID64()..".txt", "DATA" ) or "[]" ) or {}
			local addingfile = table.insert( currentfile, rcname )
			print("currentfile: "..tostring(currentfile))
			print("addingfile: "..tostring(addingfile))
			print("rcname: "..tostring(rcname))

			file.Write( "lmm_cardealer_data/"..ply:SteamID64()..".txt", util.TableToJSON( addingfile ) )
		end


the console output is this…



currentfile: table: 0x1de50328
addingfile: 1
rcname: Jeep


and still the data file is turing empty


local currentfile = util.JSONToTable( file.Read( "lmm_cardealer_data/"..ply:SteamID64()..".txt", "DATA" ) or "[]" ) or {}

You’re not planning on selling this on scriptfodder are you?

That or a job. He tries accepting jobs on sf.

no this is neither! Its for MY server and maybe even facepunch

If we see it on scriptfodder, then you know if its for scriptfodder x3