Forum

> > CS2D > Scripts > Error attempt to concatenate field
Forums overviewCS2D overview Scripts overviewLog in to reply

English Error attempt to concatenate field

8 replies
To the start Previous 1 Next To the start

old Error attempt to concatenate field

Paulo49
User Off Offline

Quote
LUA ERROR: attempt to call a nil value
LUA ERROR: sys/lua/formiga.lua:66: attempt to concatenate field '?' (a nil value)

More >

old Re: Error attempt to concatenate field

Dousea
User Off Offline

Quote
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).

old Re: Error attempt to concatenate field

GeoB99
Moderator Off Offline

Quote
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:
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.

old Re: Error attempt to concatenate field

Lee
Moderator Off Offline

Quote
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

1
x .. table[index]

in this case, the only viable candidate is only line 67 in the pasted 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

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.

old Re: Error attempt to concatenate field

Dousea
User Off Offline

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

old Re: Error attempt to concatenate field

Lee
Moderator Off Offline

Quote
@user Dousea: 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?
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview