Forum

> > CS2D > Scripts > table index is nil
Forums overviewCS2D overview Scripts overviewLog in to reply

English table index is nil

16 replies
To the start Previous 1 Next To the start

old table index is nil

J4x
User Off Offline

Quote
hi, i was making the is script and i get this error: table idex is nil.

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
----------------------------------------
--- Effects script by FN_Nemesis 
--- for more info visit: Unrealsoftware.de
--- Date: 11/2/2011
----------------------------------------
WeaponTables = {
[Machinegun] = "40",
[rifles] = "30,31,32,33,38,39",
[snipers] = "34,35,36,37",
[pistols] = "1,2,3,4,5,6",
[Smg] = "20,21,22,23,24",
[Shotguns] = "10,11"
}

addhook("attack","shake")
function shake(id)
if wpn == Machinegun then
parse("shake "..id.." 8")
end
end

addhook("attack","shake2")
function shake2(id)
if wpn == rifles then
parse("shake "..id.." 3")
end
end


addhook("attack","shake3")
function shake3(id)
if wpn == snipers then
parse("shake "..id.." 5")
end
end


addhook("attack","shake4")
function shake4(id)
if wpn == pistols then
parse("shake "..id.." 2")
end
end

addhook("attack","shake5")
function shake5(id)
if wpn == Smg then
parse("shake "..id.." 6")
end
end

addhook("attack","shake6")
function shake6(id)
if wpn == Shotguns then
parse("shake "..id.." 4")
end
end

addhook("hit","shake7")
function shake7(id)
parse("shake "..id.." 10")
end

addhook("serveraction","run")
function run(id,action)
if action == 1 then
parse("speedmod "..id.." 5")
end
end

old Re: table index is nil

Admir
User Off Offline

Quote
try these codes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Machinegun = {}
rifles = {}
snipers = {}
pistols = {}
Smg = {}
Shotguns = {}

WeaponTables = {
[Machinegun] = "40",
[rifles] = "30,31,32,33,38,39",
[snipers] = "34,35,36,37",
[pistols] = "1,2,3,4,5,6",
[Smg] = "20,21,22,23,24",
[Shotguns] = "10,11"
}

ps. I dunno that will fix, but I didn't get any error

old Re: table index is nil

Disconetted
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
----------------------------------------
--- Effects script by FN_Nemesis 
--- for more info visit: Unrealsoftware.de
--- Date: 11/2/2011
----------------------------------------
weapontypes={}
weapontypes[1].rifles={30,31,32,33,38,39}
weapontypes[2].machinegun={50}
weapontypes[3].pistols={1,2,3,4,5,6}
weapontypes[4].smg={20,21,22,23,24}
weapontypes[5].shotguns={10,11}

addhook("hit","shake")
function shake(id,source,wpn)
if wpn == weapontypes[1].rifles then
parse("shake "..id.." 8")
end
end

I didnt TEST it

old Re: table index is nil

Infinite Rain
Reviewer 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
machinegun={40},
rifles={30,31,32,33,38,39},
snipers={34,35,36,37},
pistols={1,2,3,4,5,6},
smg={20,21,22,23,24},
shotguns={10,11}


 addhook("attack","shake")
 function shake(id)
  if wpn==machinegun then
   parse("shake "..id.." 8")
  end
 end

 addhook("attack","shake2")
 function shake2(id)
  if wpn==rifles then
   parse("shake "..id.." 3")
  end
 end


 addhook("attack","shake3")
 function shake3(id)
  if wpn==snipers then
   parse("shake "..id.." 5")
  end
 end


 addhook("attack","shake4")
 function shake4(id)
  if wpn==pistols then
   parse("shake "..id.." 2")
  end
 end

 addhook("attack","shake5")
 function shake5(id)
  if wpn==smg then
   parse("shake "..id.." 6")
  end
 end

 addhook("attack","shake6")
 function shake6(id)
  if wpn==shotguns then
   parse("shake "..id.." 4")
  end
 end

 addhook("hit","shake7")
 function shake7(id)
  parse("shake "..id.." 10")
 end

 addhook("serveraction","run")
 function run(id,action)
  if action == 1 then
   parse("speedmod "..id.." 5")
  end
 end

old Re: table index is nil

J4x
User Off Offline

Quote
Thanks everyone, @factice u get wron in the table part, this is the correct one
1
2
3
4
5
6
machinegun={40},
rifles == {30,31,32,33,38,39},
snipers == {34,35,36,37},
pistols == {1,2,3,4,5,6},
smg == {20,21,22,23,24},
shotguns == {10,11}

old Re: table index is nil

Admir
User Off Offline

Quote
1 hook also ok, no need to much hook for attacks that will spam ur console on start server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
addhook("attack","shake")
function shake(id)
	if wpn==machinegun then
		parse("shake "..id.." 8")
	elseif wpn==rifles then
		parse("shake "..id.." 3")
	elseif wpn==snipers then
		parse("shake "..id.." 5")
	elseif wpn==pistols then
		parse("shake "..id.." 2")
	elseif wpn==smg then
		parse("shake "..id.." 4")
	elseif wpn==shotguns then
		parse("shake "..id.." 6")
	end
end

shorter code

old Re: table index is nil

Infinite Rain
Reviewer Off Offline

Quote
no.. this is isnt wrong,
1
2
3
4
5
6
machinegun={40}
rifles={30,31,32,33,38,39}
snipers={34,35,36,37}
pistols={1,2,3,4,5,6}
smg={20,21,22,23,24}
shotguns={10,11}
IF U WANT MY SCRIP WORKS USE MY TABLE!

old Re: table index is nil

Flacko
User Off Offline

Quote
FN_Linkin Park has written
I use it that way and says me: = expected near{
accept your errors dude


He accepted his error so he corrected his code, also wtf is this:
1
2
3
4
5
6
machinegun={40},
rifles == {30,31,32,33,38,39},
snipers == {34,35,36,37},
pistols == {1,2,3,4,5,6},
smg == {20,21,22,23,24},
shotguns == {10,11}

Do you know that the == operator is not the same as the = operator?
The == operator returns a boolean value if both of it's parameters are true, and the = operator assigns the second parameter's value to the first one.

Therefore, things like this are a complete fail:
smg == {20,21,22,23,24}

You only had to remove those extra colons from factis' code.

BTW, most of you are still missing something, you can't compare a number (the weapon) with a table. Make a function that find a value in a table instead... and WTF is wpn? You didn't declare that variable anywhere where I can see it...

old Re: table index is nil

Robotic-Brain
User Off Offline

Quote
were the hell do you init the "wpn" variable?

and (if i understand the wanted functionality right)
if wpn==snipers is wrong, too

you want a recoil effect right?

then this is the right code:

1
2
3
4
5
6
7
8
9
10
11
12
13
function searchInArray(array,value)
	for k,v in pairs(array) do
		if v==value then return true end
	end
	return false
end

function shake(id)
	wpn=player(id, "weapontype")
	if wpn==searchInArray(machinegun,wpn) then
		parse("shake "..id.." 8")
	elseif -- etc....
end

don't forget addhook and factis699s array definition

not tested...

but i would preffer this:

1
2
3
4
5
6
7
8
9
10
11
12
weaponQuakes = {}
for i=1, 6 do
	-- Pistols
	weaponQuakes[i] = 2
end

addhook("attack","shake")
function shake(id)
	if (weaponQuakes[player(id,"weapontype")] ~= nil) then
		parse("shake "..id.." "..weaponQuakes[player(id,"weapontype")])
	end
end

tested and works perfect
(of course you have to add the other weapons)


EDIT: @Falcko: I was about to say this but you were faster while I was typing

old Re: table index is nil

J4x
User Off Offline

Quote
well, thx for correcting me, i don't realize that he remove the colons, and i don't know much about lua so plz cool down guys... anyway thanks for your answers.

Ps: if this is incorrect can someone explain me why it works?
1
2
3
4
5
6
machinegun={40},
rifles == {30,31,32,33,38,39},
snipers == {34,35,36,37},
pistols == {1,2,3,4,5,6},
smg == {20,21,22,23,24},
shotguns == {10,11}

old Re: table index is nil

Flacko
User Off Offline

Quote
The code you posted is equivalent to this:
1
machinegun = {40},false,false,false,false,false

This is because lua allows for multiple assignments in a single statement like this:
1
one, two, three, four = 1,2,3,4
Or like this, in multiple lines:
1
2
3
4
one, two,three,four = 1,
2,
3,
4

The reason of all those "false" is because like I said, the == operator returns a boolean value that is true if both of it's operands are equal.
Since rifles (for example) is a nil variable because it's not defined yet, the nil == {30,31,32,33,38,39} comparison returns false.
Moreover, all those false values are not stored anywhere so you can say that they are discarded. The only assignment that actually took place was machinegun = {40}.
edited 1×, last 20.02.11 04:01:36 am

old Re: table index is nil

Lee
Moderator Off Offline

Quote
It's not syntactically incorrect, it's just meaningless as you're evaluating multiple boolean expressions after the assignment. Also, as Lua compares references by unique identification (either hashvalue or memory address), table == {...} will always evaluate to false.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview