English how to know if player had specific weapon

2 replies
Goto Page
To the start Previous 1 Next To the start
Up
khaled1968
User
Offline Off
Hey...

I was trying to make a script that allows admin to strip specific weapon from a specific player, however, It worked, but I had a problem with how to find out if that player had that weapon in the first place.

Like... I can force strip him, but i want a script that checks if the player had that weapon first or not.

And yes, i tried "playerweapons(id)" thing but it didn't work with my idk why, i don't even get error message in the console, it just doesn't work!

If someone can write that simple script for me i would be thankful, Im not lazy, I tried my best but i just didn't succeed for some reason...

I want something like !strip <id>, if that player didn't had the weapon, the sender of the stripping message will get something like: " The player you wanted to strip doesn't have "..weaponname.." ".

Just that.

Some of stupid attempts if you wanna laugh at me:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if playerweapons(id,51) then
parse("strip "..id.." 51")

if playerweapons(id,HE) then
parse("strip "..id.." 51")

if playerweapons(id,"HE") then
parse("strip "..id.." 51")

if playerweapons(id) == 51 then
parse("strip "..id.." 51")

if playerweapons(id) == "HE" then
parse("strip "..id.." 51")


Also, here is another attempt where i downloaded online script and tried to modifiy it... I was trying to make player receive message with "YES" if he had one of those 5 weapons you can see in the script...

Code:
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
hasWeapon=function(id,weapon)
for _,wpn in pairs(playerweapons(id)) do
if weapon==wpn then
return true
end
end
end
      

weapons={
{"AK",30},
{"M4",32},
{"HE",51}
}
            
addhook("say","anything")
function anything(id,txt)
for _,weapon in pairs(weapons) do
if txt == "y" then
if hasWeapon(id,weapon[2]) then
msg2(id,"YES2")
return 1
end
end
end
end
edited 3×, last 03.08.22 12:44:29 am
03.08.22 12:57:45 am
Up
Bowlinghead
User
Offline Off
cs2d lua cmd playerweapons

First, the function "playerweapons(id)" only requires 1 parameter.
It gives you all the weapons one player has.

Following code gives you a list of all weapons of the player with the ID 1:
Code:
1
local weaponList = playerweapons(1)

This list could look like this:
Quote:
weaponList = {3, 23, 34, 46}

Now you can go through every weapon the player does posess and print it like so:
Code:
1
2
3
for _, weaponId in pairs(weaponList) do
     msg("She does own Gun-ID: "..weaponId);
end

This code would print something similiar to this:
Quote:
She does own Gun-ID: 3
She does own Gun-ID: 23
She does own Gun-ID: 34
She does own Gun-ID: 46


The loop (for _,...) is needed, because you get a bunch of weapons ID and you have to cycle through each weapon individualy to find the desired one.
So in order to strip an AK47 (ID: 30) from the player id 1 you need to write:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function StripAKFromPlayer1()
     
     
     if (player(1,"exist") == false) then
          return "Player does not exist!"
     end

     local weaponList = playerweapons(1)
     for _, weaponId in pairs(weaponList) do
          if (weaponId == 30) then
               msg("We found the illegal AK on player 1. Go to jail y.y!")
               parse("strip 1 30");
               parse("setmoney 1 "..player(1,"money")-420);
          end
     end
end

Untested code!
Share time limited free games here
03.08.22 01:42:21 am
Up
khaled1968
User
Offline Off
@user Bowlinghead: Great explanation! thanks

I was able to solve the problem myself after a very large number of attempts, but I still do not understand some things, fortunately your response came in a time.

I think I do get it now after all... Can't say for sure until I try my code in harder position, but it's all good for now.
To the start Previous 1 Next To the start