Forum

> > CS2D > Scripts > LUA Question - attempt to index field
Forums overviewCS2D overview Scripts overviewLog in to reply

English LUA Question - attempt to index field

20 replies
Page
To the start Previous 1 2 Next To the start

old LUA Question - attempt to index field

Deleted User

Quote
Hello guys I need help,
Here is script:

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
function array(m)
	local array = {}
	for id = 1, m do
		array[id] = 0
	end
	return array
end

function nonil()
	local nonil
	if nonil == nil then nonil = 0 end
	return nonil
end

tnti = {}
tnt = nonil()

function tntspawn(xxx,yyy)
	local img = image("gfx/mm/tnt.bmp",xxx*32+16,yyy*32+16,0)
	tnti[img].x = {xxx*32}
	tnti[img].y = {yyy*32}
end

addhook('use', 'mm_use')
function mm_use(id,x,y)
	for i = 1, #tnti do
		if tnti[i].x == player(id, 'tilex') and tnti[i].y == player(id, 'tiley') then
			msg('TnT tAken')
		end
	end
end

addhook('say', 'D')
function D(id,t)
	tntspawn(player(id, 'tilex'), player(id, 'tiley'))
end

Error: When I say something appear error:
1
line 20, Attempt to index field '?' (a nil value)

Guys I cant fix it, please help!

old Re: LUA Question - attempt to index field

Vectarrio
User Off Offline

Quote
this error appears when you are trying to index a table/array which is nil.
1
2
3
4
5
function nonil()
     local nonil
     if nonil == nil then nonil = 0 end
     return nonil
end
that piece of code is pointless. you could just write 0.
tnt=0.
it says that tnti[img].x is nil. that's strange. That's all I can do.

old Re: LUA Question - attempt to index field

Yasday
User Off Offline

Quote
Look at this:
1
tnti[img] = {}
Now what do you think?
Right, it's missing. ( For accessing the "x" and "y" fields. )

Edit:
Wait, shit, didn't look at your code right. You're right.

Edit²:
But that "*32" is useless shit.

old Re: LUA Question - attempt to index field

Marcell
Super User Off Offline

Quote
Hi i want to create a jetpack for cs2d but something not works really. I get this error when i go to console
sys/lua/jetpack.lua:1: unexpected symbol near "i"
1
2
3
4
5
6
7
8
9
addhook("say","jetpack")
function jetpack(id,txt)
if (txt=="!jetpack") then
    freeimage(id)
	id=image("gfx/jetpack/jetpack.bmp",1,1,200+id)
parse("speedmod ..id.. 40")
parse("explosion "..player(p,"x").." "..player(p,"y").." 100 100 "..p);
end
end

old Re: LUA Question - attempt to index field

Vectarrio
User Off Offline

Quote
user Marcell has written
Hi i want to create a jetpack for cs2d but something not works really. I get this error when i go to console
sys/lua/jetpack.lua:1: unexpected symbol near "i"
1
2
3
4
5
6
7
8
9
addhook("say","jetpack")
function jetpack(id,txt)
if (txt=="!jetpack") then
    freeimage(id)
	id=image("gfx/jetpack/jetpack.bmp",1,1,200+id)
parse("speedmod ..id.. 40")
parse("explosion "..player(p,"x").." "..player(p,"y").." 100 100 "..p);
end
end

show your whole script

old Re: LUA Question - attempt to index field

Lee
Moderator Off Offline

Quote
@Marcell

1. freeimage(id)
This is incorrect, as id refers to the player id while freeimage requires an "image id", the two are quite different numbers.

2.
1
2
3
id=image("gfx/jetpack/jetpack.bmp",1,1,200+id)
parse("speedmod ..id.. 40")
parse("explosion "..player(p,"x").." "..player(p,"y").." 100 100 "..p);

This is also incorrect, id no longer references the player.

1
parse("speedmod ..id.. 40")
Should've been "speedmod " .. id .. " 40", however, as id no longer references the player, this will do absolutely nothing

1
parse("explosion "..player(p,"x").." "..player(p,"y").." 100 100 "..p);

This is the line that is throwing an exception. You never ever defined p anywhere in your code. What is p? Do you mean id?

The correct code is as follows:

1
2
3
4
5
6
7
8
9
10
addhook("say","jetpack")
player_imgs = {} -- Associate each player with an img id
function jetpack(p,txt)
	if (txt=="!jetpack") then
		freeimage(player_imgs[p] or 0)
		player_imgs[p] = image("gfx/jetpack/jetpack.bmp",1,1,200+id)
		parse("speedmod " .. p .. " 40")
		parse("explosion "..player(p,"x").." "..player(p,"y").." 100 100 "..p);
	end
end

There are still a few edge cases that you need to take care of, I'll leave this task to you to figure out.

old Re: LUA Question - attempt to index field

Marcell
Super User Off Offline

Quote
I tested with this lua but when i write into chat !jetpack
the console show this:
LUA ERROR: sys/lua/jet_pack.lua:6: attempt to perform arithmetic on global 'id' (a nil value)
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview