Forum

> > CS2D > Scripts > How to create a file??
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to create a file??

23 replies
Page
To the start Previous 1 2 Next To the start

old How to create a file??

Raaj Nadar
User Off Offline

Quote
Hey guys,

I was working on a script and i want to know how to make a file using a lua script.

Example:-
I am running a server a player joins.

I want to make a file Named by his USGN Id.
And i want to write all the details about his logs in this.

If you can help me create the directory it will be nice i will make the writing part.

sys/lua/script/data/"..player(id,"usgn")..".txt

If anyone did not understand i will give more details or explaination.

Spoiler >
edited 3×, last 06.08.14 04:12:11 pm

old Re: How to create a file??

Rainoth
Moderator Off Offline

Quote
The code >

Part of a script I was making. This should work, just change the info you want to be written.
edited 1×, last 07.08.14 05:58:04 pm

old Re: How to create a file??

Raaj Nadar
User Off Offline

Quote
I made my own save script but the problem was that when a player joins it says No files found.

Because the save script cannot create the Directory for USGN Id.

If you want i can post my script code.

old Re: How to create a file??

Rainoth
Moderator Off Offline

Quote
Just paste it as first line. It'll create a folder whenever that script is run. I would make it myself instead of using code unless you need a folder for every user.

old Re: How to create a file??

Raaj Nadar
User Off Offline

Quote
I want to save more than 15 things.
The things that are related to Server.
Like.
Player rank,last used nick,Chat color used,Mute timer left for user after leaving the server,Temp-Ban timer.

And many more things.

So i have to past the same code given by @user Infinite Rain: Without editing it.

What is this "Name goes here"

@user Rainoth: I want this system because i don't know who is going to join my server.So if a unknown player joins his stats will not be saved.

I will try and check whether it works.

I think you people mis-took me i want to make a .txt file in a specified path.I don't want to make a folder.
edited 1×, last 04.08.14 10:46:22 am

old Re: How to create a file??

Rainoth
Moderator Off Offline

Quote
@user Raaj Nadar: then don't paste the line that user Infinite Rain wrote

My script does exactly what you need. Run it and see how it works. Then all you need to do is change data to suit what you need to save yourself (cause my script saves level,exp and so on. Things that are from another mod. You probably want to save other things :))

old Re: How to create a file??

Dousea
User Off Offline

Quote
Creates file and write it:
local file = io.open ("sys/lua/statistic.txt", "w+")

file:write ("10 5 0")
file:close ()

Loads file:
local file = io.open ("sys/lua/statistic.txt", "r")
local values = {}

for value in file:read ():gmatch ("[^%s+]") do
	table.insert (values, value)
end

values[1] returns 10, values[2] returns 5 and values[3] returns 0.
For example you want to print them in console:
for key, value in pairs (values) do
	print (value)
end

It prints:
10
5
0

old Re: How to create a file??

Raaj Nadar
User Off Offline

Quote
@user Dousea: I dont want to print in console.
I just want to create a .txt file as soon as a player joins and save the sats as soon as he leaves the server.

Next time it should load the same stats that was saved and overwrite it.

I did not checked you code in Cs2D.

old Re: How to create a file??

RedizGaming
GAME BANNED Off Offline

Quote
easy:
1
2
3
4
5
6
7
8
addhook("join","_stats")
addhook("leave","_stats")

function _stats(id)
file=io.open("sys/lua/data/"..player(id,'usgn')..".txt","w")
file:write("example:player(id,'score')")
file:close()
end

old Re: How to create a file??

Bounty Hunter
BANNED Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-- do not forgot about this:
USGN_DEFAULTDATA['money'] = 0
USGN_DEFAULTDATA['score'] = 0

addhook('join','onJoin')
function onJoin(id)
	ReloadUSER(id)
	
	-- TEST --
	msg2(id, "Hello "..player(id,"name")..", you've $"..USER[id]["money"].." and "..USER[id]['score'].." score")

end

addhook('leave','onLeave')
function onLeave(id)
	-- do not forgot about this:
	USER[id]['money'] = player(id,'money')
	USER[id]['score'] = player(id,'score')
	SaveUSER(id)
end
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview