Writing info to txt file

So, I recently wanted to come up with a script for my new gamemode and it’s going well so far but I’m stuck on one thing. I am trying to write the clients info(name, steam id, and ip address) to a txt file but I want it to only write if it is their first time ever joining and I can’t find a command for a player joining for a first time ever… the closest thing is PlayerInitialSpawn but that isn’t going to give me the desired results. Here is the portion of my code so far:


function GM:PlayerInitialSpawn()
	filex.Append("data/info.txt", "Name: " .. ply:Nick() .. "Steam ID: " .. ply:SteamID() .. "IP Address: " .. ply:IPAddress() )
end

but i know that will write their info everytime they join the server which i dont want… im thinking i can use file.read to check if it is there already but i dont know how to do that. could someone write up the code for me?

It’d probably be better to use something like this:
Every time they join, check some value like “alreadyJoined” using **[Player.GetPData

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.GetPData)** and if it doesn’t exist, create it using **[Player.SetPData

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.SetPData)**, and log the player has joined. If it does exist, then do nothing.

Or you could store the SteamID’s of everyone that joins in a table on the server (sql.Query stuff), and check that on connect, and if it’s not there, log to your txt file, and add the steamid to the table.

I like the first idea but have no clue how to code that with the checking if its already there. Could you do that for me?

I personally would just do:

[lua]
function GM:PlayerInitialSpawn( ply ) – you forgot the parameter
if( !string.find( file.Read(“info.txt”), ply:SteamID() ) ) then – check if their steamid has been logged before
filex.Append(“data/info.txt”, “Name: " … ply:Nick() … " || Steam ID: " … ply:SteamID() … " || IP Address: " … ply:IPAddress()…”
" ) --create/update file
end --end if statement
end --end function
[/lua]

the !string.find was what i have been looking for! thanks c-unit

also, i dont understand that parameter thing… how do you know what to put in there and how do you know when to put ply in there?

no problem

[editline]25th June 2011[/editline]

parameters are jsut variables, so ply can be anything. But to know which functions need parameters look up the function on this website. So on the hook DoPlayerDeath the hook pushes 3 parameters. the player about to die, the person who killed the user, and the damageinfo.

so basically

[lua]function GM:DoPlayerDeath( ply, attacker, dmginfo )
print(ply, attacker, dmginfo)
end[/lua]

and

[lua]function GM:DoPlayerDeath(p_xdxdlol, att_xdxdhurr, dmg_infololol)
print(p_xdxdlol, att_xdxdhurr, dmg_infololol)
end[/lua]

will output the exact same information, since the parameters are just variables assigned to the information the hook pushes when its called

[editline]25th June 2011[/editline]

this may be a bit advanced, but in c++ when Garry calls a hook, it will be something similar to this( well this is how I would do it anyways )

[cpp]ILuaInterface* lua;

lua->Push(lua->GetGlobal(“hook”)->GetMember(“Call”));
lua->Push(“ExampleHook”);
lua->PushNil();
lua->Push( player );
lua->Push( steamid );

lua->Call(4, 0);[/cpp]

so if you were to check every frame for when a player attacked another player and called this hook( obviously while saving the player and steamid information to the palyer & steamid variables ) it would create the new hook with this information it has for you basically( easiest way to explain ). So in lua with that code( assuming you added it where it was applicable) you could hook it and do:

[lua]hook.Add(“ExampleHook”, “uniqueidentifier”, function(ply, steamid)
print(ply:Nick()…" with the SteamID “…steamid…” attacked another user!")
end)[/lua]

[editline]25th June 2011[/editline]

I hope parameters and hooks make a little more sense now?

[editline]25th June 2011[/editline]

oh and I forgot to say, ‘ply’ is not some special word in lua, the only thing that makes it special is that people use ply and pl a lot in their lua while assigning it to the player the hook pushes. ply without being a parameter or a local var above is nil and means nothing. parameters are your key to get player information without looping through the player table and singling out the player

Use glon.encode to write strings to a file and glon.decode to read the string from the file.

Basically make a large table that you can save information to for instance

[lua]local LargeTable = {}
if file.Exists( “LargeTable.txt” ) then
LargeTable = glon.decode( file.Read( “LargeTable.txt”, true ) )
end[/lua]

[lua]hook.Add( “PlayerJoinedYattaYatta”, “Somerandomname”, function( ply, steamid )
table.insert( LargeTable, { ply:Nick(), ply:SteamID(), ply:IPAddress() } )
file.Write( “LargeTable.txt”, glon.encode( LargeTable ) --Save the table.
end )[/lua]

to save the file you can use this hook

[lua]hook.Add( “ShutDown”, “LargeTable.ShutDown”, function()
file.Write( “LargeTable.txt”, glon.encode( LargeTable )
end )[/lua]