Writing data to a log.txt.

Hello! i was wondering how you would write data like a string and numbers to a txt file. I know ulx does something similar but i dont know how.

Thanks! i tried this:
file.Write( “saleslog.txt” , “/nPlayer “…ply:Nick()…” Just Sold A “…curr_class…” For “…_f_kp…”$!” )
and nothing…:frowning:

If I get your meaning correctly, you want to store information and have it retrievable, without the hassle of encoding it? If so, GMod ships natively with a JSON library that is easy to use, and can be read across different applications easily because of the widespread implementation.

util.TableToJSON is the function you are looking for if this is the case. You give a table as the first argument, and the function will return a string with the information of the table encoded within. You can then save this in a file using


To retrieve the information, you would read the file using

file.Read, and decode the resulting string with

util.JSONToTable. This should then return a table with the information you gave it previously.

This does however have limitations. You cannot store userdata, or any other information apart from the primitive types (string, number, vector, angle, etc). There are appropriate examples for how to use these functions if you follow the links provided.

Thanks for the help all!
i just got it working though…
here is my code:

		if _log_start == true then
		_log_start = false
			file.Write( "saleslog.txt" , "
Player "..ply:Nick().." Just Sold A "..curr_class.." For ".._f_kp.."$!" )
			file.Append( "saleslog.txt" , "
Player "..ply:Nick().." Just Sold A "..curr_class.." For ".._f_kp.."$!" )

You should check using a file.Exists rather than _log_start variable. If the file exists, Append. If no, Write.

Thanks! i will look into it!

If you want to easily store tables and have them be encoded and decoded for you without you doing much extra work I would suggest using pON. It’s faster than Garry’s Mod’s JSON thing and vON.

[editline]29th July 2016[/editline]

Also, depending on what it is you might want to use Garrys Mod’s sql library instead.

I just wanted to make a log file and I got it working flawlessly! Thanks for the support and help!