Solved Checking if a player has an item in their inventory (C#)

salva

Friendly self-taught developer
Administrator
Jan 31, 2016
577
613
63
I do not know exactly how to find out if an inventory contains something, the only thing that has occurred to me is the following code but it does not work. Does anyone know the right way to do this? Thank you

C#:
  if (player.Inventory.Items.Contains("M4")) //ERROR this line
               {
                   player.Message("you inventory contains M4");
               }
               else
               {
                   player.Message("you inventory NO contains M4");
               }
 

tarynkelley

Retired Staff
Retired Staff
Trusted Member
Nov 14, 2015
575
178
28
Parts Unknown
Try

C#:
  if (player.Inventory.HasItem("M4"))
               {
                   player.Message("you inventory contains M4");
               }
               else
               {
                   player.Message("you inventory NO contains M4");
               }
 
  • Like
Reactions: salva

salva

Friendly self-taught developer
Administrator
Jan 31, 2016
577
613
63
Try

C#:
  if (player.Inventory.HasItem("M4"))
               {
                   player.Message("you inventory contains M4");
               }
               else
               {
                   player.Message("you inventory NO contains M4");
               }
Amazing !!!! , I had not tried that method. I'm too noob.

Works perfectly, thanks tarynkelley. ;)