Forum

> > CS2D > General > Table with player-weapontype on attack hook
Forums overviewCS2D overviewGeneral overviewLog in to reply

English Table with player-weapontype on attack hook

2 replies
To the start Previous 1 Next To the start

old Table with player-weapontype on attack hook

Bowlinghead
User Off Offline

Quote
Good morning community,

I´m a littlebit confused about my problem:

I´m running this small script:
1
2
3
4
5
6
7
8
9
-- Just the numbers from 1 to 91
table1 = {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,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91}

addhook("attack","fame")
function fame(id)
		-- string.char(169) = © ((C)) - Just for the colour. Ignore it!
		msg2(id,string.char(169).."255255255Your Weapon-ID: "..table1[player(id,"weapontype")].."@C")
	end
end
I know, the script makes no sense

The problem:

Usually, on every attack the script says:
Your Weapon-ID: <weapontype>

If you shot with an USP then it says:
Your Weapon-ID: 1

If I´m throwing my HE (ID: 51) then this error appears in the console:
1
LUA ERROR: sys/lua/test.lua:6: attempt to concatenate field '?' (a nil value)
So, I thougt: If I throw my HE, I don´t have it in my hand anymore. So my weapon has no ID anymore and the script can´t compare it with the table.

Then I thought, I change the code a littlebit:
1
2
3
4
5
6
7
8
9
10
-- Just the numbers from 1 to 91
table1 = {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,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91}

addhook("attack","fame")
function fame(id)
	-- 51 = HE, 52 = Flashbang, etc. almost every grenade
	if player(id,"weapontype")~=51 or player(id,"weapontype")~=52 or player(id,"weapontype")~=53 or player(id,"weapontype")~=54 or player(id,"weapontype")~=75 or player(id,"weapontype")~=76 or player(id,"weapontype")~=77 or player(id,"weapontype")~=86 or player(id,"weapontype")~=87 or player(id,"weapontype")~=89 then
	msg2(id,string.char(169).."255255255Your Weapon-ID: "..table1[player(id,"weapontype")].."@C")
     end
end
But, the same error appears again although there is the "if" command...

If my code is like this:
1
2
3
4
addhook("attack","fame")
function fame(id)
	msg2(id,string.char(169).."255255255Your Weapon-ID: "..player(id,"weapontype").."@C")
end
And I through a grenade then it says without any error:
Your Weapon-ID: 0

Thanks for reading this!

old Re: Table with player-weapontype on attack hook

DC
Admin Off Offline

Quote
Isn't the error obvious? It returns 0 because you have nothing in your hand after throwing a grenade.

Your table has no entry at the index 0 (Lua tables start at index 1). So the value of table1[0] is nil. nil can not be concatenated with other strings. This leads to the error.

Your if condition doesn't change anything because player(id,"weapontype") is 0. Change it to
1
if player(id,"weapontype")~=0 then
and your problem should be fixed.

(I also don't really understand why you are using the table. It's pretty pointless)
edited 1×, last 22.07.13 11:52:22 am
To the start Previous 1 Next To the start
Log in to replyGeneral overviewCS2D overviewForums overview