Forum

> > CS2D > Scripts > How to create a file??
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch How to create a file??

23 Antworten
Seite
Zum Anfang Vorherige 1 2 Nächste Zum Anfang

alt How to create a file??

Raaj Nadar
User Off Offline

Zitieren
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 >
3× editiert, zuletzt 06.08.14 16:12:11

alt Re: How to create a file??

Rainoth
Moderator Off Offline

Zitieren
The code >

Part of a script I was making. This should work, just change the info you want to be written.
1× editiert, zuletzt 07.08.14 17:58:04

alt Re: How to create a file??

Raaj Nadar
User Off Offline

Zitieren
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.

alt Re: How to create a file??

Rainoth
Moderator Off Offline

Zitieren
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.

alt Re: How to create a file??

Raaj Nadar
User Off Offline

Zitieren
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.
1× editiert, zuletzt 04.08.14 10:46:22

alt Re: How to create a file??

Rainoth
Moderator Off Offline

Zitieren
@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 :))

alt Re: How to create a file??

Dousea
User Off Offline

Zitieren
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

alt Re: How to create a file??

Raaj Nadar
User Off Offline

Zitieren
@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.

alt Re: How to create a file??

RedizGaming
GAME BANNED Off Offline

Zitieren
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

alt Re: How to create a file??

Bounty Hunter
BANNED Off Offline

Zitieren
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
Zum Anfang Vorherige 1 2 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht