Forum

> > CS2D > Scripts > Lua report has error
Forums overviewCS2D overview Scripts overviewLog in to reply

English Lua report has error

5 replies
To the start Previous 1 Next To the start

old Lua report has error

cELL
User Off Offline

Quote
Hi friends of unrealsoftware. i need help with the lua of report.
I have an error with this lua, When I put.. Example: !report 3 speed hacks, I receive an error It is not working well, What is not working well is that the message must appear "Player does not exist" If there is not a player with that ID. Here is code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
path = "sys/lua/reports.txt"

pl = {}
addhook("join", "_join")
function _join(id)
     pl[id] = {}
     pl[id].usgn = player(id,"usgn")
end

addhook("say","_say")
function _say(id, txt)
     if txt:sub(1,7)=="!report" then
          t = tonumber(txt:sub(8,9))
          r = txt:sub(10)
          if t~=nil and r~= nil then
               if t~=id then 
                    if pl[id].usgn > 0 then
                         if player(id,"exists") then
                              if not player(t,"bot") then
                                   if r:len() > 5 then
                                        report(id, t, r)
                                        return 1
                                   else
                                        msg2(id,"\169255150150Error: \169255255255Please enter a valid reason")
                                        return 1
                                   end
                              else
                                   msg2(id,"\169255150150Error: \169255255255You cannot report a bot")
                                   return 1
                              end
                         else
                              msg2(id,"\169255150150Error: \169255255255Player does not exist")
                              return 1
                         end
                    else
                         msg2(id,"\169255150150Error: \169255255255You have to be logged into USGN to report a player")
                         return 1
                    end
               else
                    msg2(id,"\169255150150Error: \169255255255You cannot report yourself")
                    return 1
               end
          else
               msg2(id,"\169255150150Usage: \169255255255!report <id> <reason>")
               return 1
          end
     end
end

function report(id, t, r)
     f = io.open(path, "a+")
     f:write(os.date().. ": "..player(id,"name").." ("..pl[id].usgn..") reported "..player(t,"name").." ("..pl[t].usgn.."|"..player(t,"ip")..") Reason: "..r.."\n")
     f:close()
     msg2(id,"\169255255255Info: You successfully reported \169175255100"..player(t,"name"))
end

And the error that appears to me
IMG:https://i66.tinypic.com/112bp05.jpg

old Re: Lua report has error

Rainoth
Moderator Off Offline

Quote
Line 18.
Change 'id' into 't'
id is the player id of the person who says the message.
t is the player id of the person whose id is written in the message.

It has to be
if player(t,"exists") then


Good luck.

old Re: Lua report has error

Cure Pikachu
User Off Offline

Quote
In addition, I will not recommend doing this
t = tonumber(txt:sub(8,9))
r = txt:sub(10)

because the code will fail if the ID is 10 or higher (i.e. double-digit figures). Better way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-- Put this after line 8/9
function toTable(t,match)
	local cmd = {}
	if not match then
		match = "[^%s]+"
	else
		match = "[^"..match.."]+"
	end
	for word in string.gmatch(t, match) do
		table.insert(cmd, word)
	end
	return cmd
end

-- Replace line 13-14 with the following
local s = toTable(txt)
local t = tonumber(s[2])
local r
for i, x in ipairs(s) do
	if i >= 3 then
		if r == "" then
			r = x
		else
			r = r.." "..x
		end
	end
end
edited 1×, last 15.12.16 02:15:47 am

old Re: Lua report has error

Rainoth
Moderator Off Offline

Quote
That's because you take txt:sub(8,9). You should take 9,10 because that's where the id is stored. In other words, you just made a slight mistake in determining what part of text is id

"!report 3 reason"
#"!report" == 7
so then:
1 !
2 r
3 e
4 p
5 o
6 r
7 t
8 space (whitespace)
9 is first digit of id
10 is second digit of id
11 is the second space (whitespace)
12 is the beginning of the reason string.

How to get rid of all this mess? Don't use txt:sub(). Instead, use txt:split(). Dig the forums here a bit and you'll find it.

Good luck.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview