English I.O (lua)

10 replies
Goto Page
To the start Previous 1 Next To the start
29.05.13 08:14:29 pm
Up
limonata
User
Offline Off
Hi guys i made a script but it doesnt work where is my mistake?

Code:
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
29.05.13 08:17:30 pm
Up
Flacko
User
Offline Off
You didn't use any indentation. That's a huge mistake.
29.05.13 08:19:21 pm
Up
ohaz
User
Offline Off
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
https://ohaz.engineer - Software Engineering
29.05.13 08:51:03 pm
Up
wotaan
User
Offline Off
Also have problem on line 6
Code:
1
local msj = string.sub(8,50)

it should be
Code:
1
local msj = string.sub(txt,8)
IMG:http://x-gameservers.net/svlist/cs2d/109.73.77.38:35115.png
IMG:http://rj.revolvermaps.com/h/b/26/91hkrxy2xvp.png
29.05.13 09:05:18 pm
Up
Alistaire
User
Offline Off
Code:
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
IMG:http://i.imgur.com/5zhwOTP.png
29.05.13 09:07:00 pm
Up
Infinite Rain
Reviewer
Offline Off
Why don't you tab your script?
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV
29.05.13 09:09:10 pm
Up
limonata
User
Offline Off
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:
Code:
1
file:write('\nReport @ ['..os.date('%X %x')..']:"'..txt..'"')
29.05.13 09:16:43 pm
Up
wotaan
User
Offline Off
Code:
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
IMG:http://x-gameservers.net/svlist/cs2d/109.73.77.38:35115.png
IMG:http://rj.revolvermaps.com/h/b/26/91hkrxy2xvp.png
29.05.13 09:16:44 pm
Up
Alistaire
User
Offline Off
user wotaan has written:
Code:
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:
Code:
1
file:write('\nReport @ ['..os.date('%X %x')..']:"'..txt..'"')


Make the folder 'reports' in sys/lua
IMG:http://i.imgur.com/5zhwOTP.png
29.05.13 09:32:30 pm
Up
limonata
User
Offline Off
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