Forum

> > CS2D > Scripts > I.O (lua)
Forums overviewCS2D overview Scripts overviewLog in to reply

English I.O (lua)

10 replies
To the start Previous 1 Next To the start

old I.O (lua)

limonata
User Off Offline

Quote
Hi guys i made a script but it doesnt work where is my mistake?

1
2
3
4
5
6
7
8
9
10
11
12
local file = io.open("sys/lua/report.txt","w")

addhook("say","report")
function report(id,txt)
if string.sub(txt,1,6) == "!report" then
local msj = string.sub(8,50)
file:write(msj)
file:close()
msg2(id,"Your message send!")
return 1
end
end

old Re: I.O (lua)

Flacko
User Off Offline

Quote
You didn't use any indentation. That's a huge mistake.

old Re: I.O (lua)

ohaz
User Off Offline

Quote
first of all: using file:close() in the say function leads to the script not being able to write anything to the file after you've written something for the first time

old Re: I.O (lua)

wotaan
User Off Offline

Quote
Also have problem on line 6
1
local msj = string.sub(8,50)
it should be
1
local msj = string.sub(txt,8)

old Re: I.O (lua)

Alistaire
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
addhook('say', 'sayhook')

function sayhook(id, txt)
	if txt:sub(1, 7) == '!report' then
		writeToReports(id, txt:sub(8))
		msg2(id, 'Succesful')
	end
end

function writeToReports(id, txt)
	local file = io.open('sys/lua/reports/'..player(id, 'usgn')..'.rep', 'a+')
	file:write('\nReport @ ['..os.date('%X %x')..']:"'..txt..'"')
	file:close()
end

old Re: I.O (lua)

limonata
User Off Offline

Quote
Thanks for your helps. And @user Alistaire: may you tell me the "a+" and ".rep" why did u use them ?



Edit: There is an error:

LUA ERROR: sys/lua/report.lua:11: attempt to index local 'file' (a nil value)

Line 11:
1
file:write('\nReport @ ['..os.date('%X %x')..']:"'..txt..'"')

old Re: I.O (lua)

wotaan
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
reportfile="sys/lua/report.txt"

addhook("say","report")
function report(id,txt)
	if (string.sub(txt,1,6) == "!report") then
		local file=io.open(reportfile,"w")
		file:write("\n"..player(id,"name").."("..player(id,"usgn")..") reported "..string.sub(txt,8))
		file:close()
		msg2(id,"Okey, i will take care of it....")
	return 1
	end
end

"a+" append update mode, previous data is preserved, writing is only allowed at the end of file.
There must be a file for update it.

More help about IO : http://lua-users.org/wiki/IoLibraryTutorial

old Re: I.O (lua)

Alistaire
User Off Offline

Quote
user wotaan has written
1
2
3
4
5
6
7
8
9
10
11
12
reportfile="sys/lua/report.txt"

addhook("say","report")
function report(id,txt)
	if (string.sub(txt,1,6) == "!report") then
		local file=io.open(reportfile,"w")
		file:write("\n"..player(id,"name").."("..player(id,"usgn")..") reported "..string.sub(txt,8))
		file:close()
		msg2(id,"Okey, i will take care of it....")
	return 1
	end
end

"a+" append update mode, previous data is preserved, writing is only allowed at the end of file.
There must be a file for update it.

More help about IO : http://lua-users.org/wiki/IoLibraryTutorial


That would not work. You're using write mode.

----

user limonata has written
Thanks for your helps. And @user Alistaire: may you tell me the "a+" and ".rep" why did u use them ?


a+ is append mode +, which adds lines to the end of the file. What you did was erase all reports and create a new one, because you used w, edit mode.

The savefiles are .rep - this is just because it looks great. You can open them with notepad, because .rep is not an actual file type.

user limonata has written
Edit: There is an error:

LUA ERROR: sys/lua/report.lua:11: attempt to index local 'file' (a nil value)

Line 11:
1
file:write('\nReport @ ['..os.date('%X %x')..']:"'..txt..'"')


Make the folder 'reports' in sys/lua

old Re: I.O (lua)

limonata
User Off Offline

Quote
Thank you Alistaire its working now

----------------
Edit:

@user Alistaire: May you add this function?

When i write !showreport i wanna see how much report posted today. Is it possible?
edited 1×, last 05.06.13 01:47:19 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview