Forum

> > CS2D > Scripts > Detecting and stripping a weapon from inventory
Forums overviewCS2D overview Scripts overviewLog in to reply

English Detecting and stripping a weapon from inventory

7 replies
To the start Previous 1 Next To the start

old Detecting and stripping a weapon from inventory

Casper-
User Off Offline

Quote
My goal is to strip a weapon from a player inventory, which he doesn't have currently in hands
>
parse("strip "..id.." "..player(id,"weapontype").."")


I've tried using :
1
2
3
4
local items = playerweapons(id)
	for _,var in pairs(items) do
		if var == "ak47" and player(id,"score") >= Shop_AK47 then
...

Used this as a reference from website
https://stackoverflow.com/questions/656199/search-for-an-item-in-a-lua-list

These are snippets of my code where this is happening :
Try 1 :
Spoiler >

Try 2 :
Spoiler >

Try 3 :
Spoiler >


So, how do I access the playerweapons table of the player(buyer) and detect the ak47 if its not in hands but in inventory, the goal is just to give ammo to ak47.

old Re: Detecting and stripping a weapon from inventory

DC
Admin Off Offline

Quote
I'm afraid the cs2d lua cmd playerweapons documentation wasn't clear enough about it but the sample there should clarify that the table doesn't contain the names of the weapons but the type IDs. I adjusted the documentation to make that more clear.

These numbers here will be returned
IMG:https://cs2d.com/img/ref_items.png


So the problem with your code is
if var == "ak47"
will never be true because var contains a number (the item type ID). And even if it would contain the name, it would be "AK-47" and not "ak47" (see image above).

The fix would be to either use
if var == 30
or
if itemtype(var, "name") == "AK-47"
.

old Re: Detecting and stripping a weapon from inventory

Casper-
User Off Offline

Quote
Thanks for the help.
Tried replacing it, still gives me Try 2 outcome. Now, I've tried to isolate the code and see if it even functions. It doesnt at all, unless I have a knife which tells me to switch to another weapon.
The code now :
Spoiler >

Wth am I doing wrong.
edited 1×, last 23.06.24 01:26:15 pm

old Re: Detecting and stripping a weapon from inventory

Bowlinghead
User Off Offline

Quote
You can always print stuff.

So for example, you can rewrite your code to something like this:
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
...
print("x: "..xtile.." / y:"..ytile)
...
elseif xtile == 13 and ytile == 7 then
		print("Wpntype in hand: "..player(id,"weapontype"))
		--weapons which are not allowed to trade
		--50 KNIFE
		--69 MACHETE
		--51 HE
		--73 MOLOTOV
		if player(id,"weapontype")==50 or player(id,"weapontype")==69 or player(id,"weapontype")==51 or player(id,"weapontype")==73 then
			msg2(id,"©255000000Switch to a pistol or another weapon!")
		else
			local items = playerweapons(id)
			print(items)
			for _,var in pairs(items) do
				print("Weapon: "..var.." found in "..player(id,"name"))
			if var == 30 then
				print("Player has AK!")
				if (player(id,"score") >= Shop_AK47) then
				    parse("equip "..id.." 30")
				    parse("setscore "..id.." "..(player(id,"score")-Shop_AK47).."")
				else 
					msg2(id,"AK47Points needed: "..Shop_AK47)
				end
			end
			end
		end
	end
...

old Re: Detecting and stripping a weapon from inventory

mrc
User Off Offline

Quote
Make a primary or secondary weapon disappear when switching weapons? This is very easy. If you are doing this in hook buy, you need to use a timer with delay 0, in addition, check if the player is in a purchase zone, if the player is within the purchase time, if the player has money to make the purchase, and if he is not buying a weapon he already has. This can be done in less than a minute.

old Re: Detecting and stripping a weapon from inventory

Cure Pikachu
User On Online

Quote
A Nazi Zombies-esque kind of game mode, hmm. Maybe like so? The
checkwpn
function written here returns true if it finds the weapon with weapon ID (wpn) in target player's inventory (id), otherwise false
EDIT: Also added one that makes another array that filters out all other items except your primary/secondary weapons if this is about enforcing the 2-gun limit that is typical for a CoD game
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
function checkwpn(id,wpn)
	for _, w in pairs(playerweapons(id)) do
		if w == wpn then
			return true
		end
	end
	return false
end

function playerguns(id)
	local pt = {}
	for _, w in pairs(playerweapons(id)) do
		local it = itemtype(w,"slot")
		if it == 1 or it == 2 then
			table.insert(pt,w)
		end
	end
	return pt
end

addhook("use","testuse")
function testuse(id,event,data,xtile,ytile)
	if xtile == 13 and ytile == 7 then -- or where ever is adjacent to the AK on the wall
		-- weapons which are not allowed to trade
		-- 50 KNIFE
		-- 69 MACHETE
		-- 51 HE
		-- 73 MOLOTOV
		local cw = player(id,"weapontype")
		if cw==50 or cw==69 or cw==51 or cw==73 then
			msg2(id,"©255000000Switch to a pistol or another weapon!")
		else
			if player(id,"score") >= Shop_AK47 then -- check if you can afford AK
				local pg = playerguns(id)
				if #pg >= 2 and not checkwpn(id,30) then -- check if you don't have AK in inventory AND you have 2 guns already
					parse("strip "..id.." "..cw)
					msg2(id,"©255000000function 1")
				else
					msg2(id,"©255000000function 2")
				end
				-- either way you are gonna give the weapon (at full ammo) and take the cost
				parse("equip "..id.." 30")
				parse("setscore "..id.." "..(player(id,"score")-Shop_AK47).."")
			else
				msg2(id,"©255000000Not enough money!")
			end
		end
	end
end
edited 4×, last 25.06.24 01:19:51 am
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview