File Creating/Writing

Hello,

So I’m working on a new addon and need a little help.

When a user logins, it prints in chat a message currently. I also have it set to create a file directory. Everytime the user clicks the ‘login’ button, I would like it to create a .txt with the username of the person who logged in and the time.

Thank you, If you don’t understand, ask below.

Thanks!

Um, use file.Write?
EDIT: Where do you want to create a file: on client or on server?

I’ve used file.CreateDir which should create a directory in the servers data folder. I then want to use file.Write but I don’t know how to make the file automatically title it’s self.

Everytime someone clicks the button and the file Is wrote, I want the title to contain the users steam name and the time the file was created.



file.Write("somedir/"..ply:Name()..".txt", "content here")


Although I strongly advise NOT to do this because player’s name may contain illegal characters that can’t be used as a part of file name and this will screw everything up.

Ok how about this. The filename is the date and time they logged in and inside the file (files content) is their steam name?

Why would you use this and not steamid? Just save it with their id and write stuff in there

Yeah, SteamID is permanent but nick is not, youre gonna have hard time tracking stuff if the player changed nick.

Did you miss the part where I said you should not do it that way? SteamID is indeed what you should use but you cannot use it directly either, colon isn’t allowed in file names. This is how you do it properly:



local fn = ply:SteamID():gsub("%W", "_")
file.Write("somedir/"..fn..".txt", "contents")


Or you can use SteamID64.

Thank you very much!

Im testing this in single player, when I put the addon on my server, will it write the file to the servers directory which I would like?

If you call file.Write on a client it will write onto client’s computer. If you call it on server it’ll write it on server computer.

In singleplayer, you are the server so it writes it on you, on server it writes to its own files, but if you run that clientside then it will be written to client. So, Yes.

[editline]16th August 2015[/editline]

Ninja’d

No, you’ll need to send the contents you want to save to the server via the net library and write the file inside net.Recieve

Ok thank you everyone.