Forum

> > CS2D > Scripts > Attempt to index field '?' a nil value
Forums overviewCS2D overview Scripts overviewLog in to reply

English Attempt to index field '?' a nil value

14 replies
To the start Previous 1 Next To the start

old Attempt to index field '?' a nil value

Infinite Rain
Reviewer Off Offline

Quote
Help I need help with a script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
addhook("say","sl_getstore")
function sl_getstore(id,t)
for s=1,#itemshop do
if btp(id,itemshop[s].x1-1,itemshop[s].y1-1,itemshop[s].x2+1,itemshop[s].y2+1,2) then
if t=="!buy" then
s='Item shop'
for h=1,2 do					   
 -> Here error <-s=s..','..itemshop[s].item[h].name
end
menu(id,s)
end
end
end
end

error at line 10 attempt to index field '?' (a nil value)

old Re: Attempt to index field '?' a nil value

Infinite Rain
Reviewer Off Offline

Quote
ok it here:
1
2
3
4
5
6
itemshop={}
itemshop[1]={x1=0,y1=0,x2=2,y2=2,buttons=1}
itemshop[1].item={}
itemshop[1].item[1]={id=32,cost=200,name="M4A1"}
itemshop[1].item[2]={id=32,cost=200,name="M4A1"}
itemshop[1].item[3]={id=32,cost=200,name="M4A1"}

old Re: Attempt to index field '?' a nil value

Robotic-Brain
User Off Offline

Quote
what do you misunderstand?

1
2
3
4
s='Item shop'
for h=1,2 do                          
	s=s..','..itemshop[s].item[h].name
end

is the same as:

1
2
s='Item shop,'..itemshop['Item shop'].item[1].name
s=s..','..itemshop[s].item[2].name

and this two keys does not exist

old Re: Attempt to index field '?' a nil value

Yasday
User Off Offline

Quote
It's because of the itemshop[s], it changes in the loop, but you want to call the same table. It hasn't got the same index, because you changed s.

1
2
3
4
5
s='Item shop'
local a = itemshop[s]
for h=1,2 do
	s=s..','..a.item[h].name
end
should work.

old Re: Attempt to index field '?' a nil value

Vectarrio
User Off Offline

Quote
@Danny:
yes, but not in this case.


@factis:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
addhook("say","sl_getstore")
function sl_getstore(id,t)
for is=1,#itemshop do
if btp(id,itemshop[is].x1-1,itemshop[is].y1-1,itemshop[is].x2+1,itemshop[is].y2+1,2) then
if t=="!buy" then
s='Item shop'
for h=1,2 do                         
s=s..','..itemshop[is].item[h].name
end
menu(id,s)
end
end
end
end
see, I changed all "s"s indexes and "s" in the for to "is"s
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview