English Error attempt to concatenate field

8 replies
Goto Page
To the start Previous 1 Next To the start
Up
Paulo49
User
Offline Off
LUA ERROR: attempt to call a nil value
LUA ERROR: sys/lua/formiga.lua:66: attempt to concatenate field '?' (a nil value)

More >
Paulo Henrique
26.01.16 10:07:13 am
Up
Gaios
Security Supporter
Offline Off
You're welcome.
26.01.16 11:02:15 am
Up
THEMUD
User
Offline Off
Would you please put it on a code tag? Because I'm not going to bother myself to count to 66.
And give some further information.
I don't know what I'm doing with my life.
26.01.16 02:37:48 pm
Up
Ortimh
User
Offline Off
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function initArray(m)
     local array = {}
     for i = 1, m do
          array[i]=0
     end
     return array
end

estiver=initArray(32)
descarregar=initArray(32)

function contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end

addhook("usebutton","butts")
function butts(id,tx,ty)
if (x==BUTTONTILEX and y==BUTTONTILEY) then
      msg2(id,'©000255000Folha coletada com sucesso.')
estiver[id] = 1
hats(id)
      end
end

timer(5000, "onSay",id, txt)
addhook("say","OnSay")
function OnSay(id,txt)
--do stuff here in 5s
end

addhook("spawn","_OnSpawn")
function _OnSpawn(id)
hats(id)
id=image("gfx/formiga/formiga1.bmp",1,1,200+id)
end

addhook("serveraction","example")
function example(id, act)
if act == 1 then
menu(id,'Menu da Formiga,Descarregar')
end
end

addhook("menu","example2")
function example2(id,title,button)
if title == "Menu da Formiga" then
if button == 1 then
if estiver[id] == 1 then
                descarregar[id] = descarregar[id] +1
                estiver[id] = 0
                                hats(id)
                else
                msg2(id,'©252000000Você está sem folhas para entregar.')
                end
end
end
return 1
end

timer(7000,"hats")
function hats(id)
parse('hudtxt2 '..id..' 0 "©000255000Folhas Entregues: '..descarregar[id]..'" 450 220')
if estiver[id] == 0 then
parse('hudtxt2 '..id..' 1 "©255000000Você não está carregando folhas" 1 380')
end
if estiver[id] == 1 then
parse('hudtxt2 '..id..' 1 "©000255000Você está carregando folhas" 1 380')
end
end

Based on your code, line 66 is only function declaration, no actual attempt to concatenate a field (which is nil value).
I'm awesome ... and I really like cookies.
26.01.16 03:47:32 pm
Up
GeoB
Moderator
Offline Off
In terms of code optimization, you haven't tabbed the code correctly (from 54 to 59) and neither the whole code is tabbed actually, apart from the array function declaration till 7 line.

Nobody would be arsed to read such a code like this. On the other hand, I am fairly sure that Lua triggers more than the actual (attempt to concatenate field <a nil value>) issue because there are some junk lines there. The most that intrigues me is this:
Code:
1
hats(id)

Could you tell me what's the purpose of that line? From my perspective, this line isn't defined anywhere or doesn't bring a value thus resulting in nil.
28.01.16 05:07:34 am
Up
Lee
Moderator
Offline Off
hats
is defined at the very end.

From the looks of the error message, the "concatenation of a nil field" suggests that the pattern we're looking for is

Code:
1
x .. table[index]


in this case, the only viable candidate is only line 67 in the pasted code

Code:
1
parse('hudtxt2 '..id..' 0 "©000255000Folhas Entregues: '..descarregar[id]..'" 450 220')


On the other hand, your
attempt to call a nil value
exception is caused by a typo.

On line 30, you wrote

Code:
1
timer(5000, "onSay",id, txt)


Unfortunately, you named your method OnSay with a capital O. Also note that id and txt are undefined at this point of the program, so you will always get id, txt = nil, nil.
28.01.16 09:52:13 am
Up
Ortimh
User
Offline Off
And, based on my observation, at line 65 you're trying to call hats function through cs2d lua cmd timer with no argument, which means when called id in hats function is the default value (x) when no argument passed through cs2d lua cmd timer (probably an empty string). At line 67 when called through cs2d lua cmd timer descarregar with index id (x) is a nil value (not exists), so you're attempting to concatenate a field which is a nil value.
I'm awesome ... and I really like cookies.
28.01.16 06:52:51 pm
Up
Lee
Moderator
Offline Off
@user Ortimh: Yep, that would be the most emergent trace that leads to the error trace the OP posted. Unbeknownst to most people, nil is a valid key, in fact every object in Lua is hashable and are in fact valid keys.

Now, the two exceptions points to a mental disconnect: you want to connect "call action HAT for user id every 5 seconds" between the timer call and the HAT function. This is correct. However, you've failed to account for the existence of the user id at the time of starting your timer, which means that HAT doesn't know who it should configure hats for.

At this junction, if I were you, I would ask myself two questions:

1. What is the behavior of hats that I want to control? Explicitly, where should the user ids be injected into the mechanism?
2. What is the best way to eliminate these errors?
29.01.16 05:39:11 am
Up
Ortimh
User
Offline Off
@user Lee: I thought nil is not a valid index for table. Thanks for telling me that. But even nil is valid, no one will use it as index anyway.
I'm awesome ... and I really like cookies.
To the start Previous 1 Next To the start