When you add a script or command, click 'if' and then from the dropdown select 'if object is in player inventory'.
I cant post a screenshot, but I'm sure you can find it if you poke around

you can also select 'if player is not carrying' for the reverse effect.