Skript inventory check not working

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the community!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Jun 23, 2023
32
0
6
function buyItem(p: player,n: number = 1):
if {_p}'s inventory contains {item::{_n}::emoji_stuck_out_tongue:rice} of sunflower named "&e&lTrade Token":
remove {item::{_n}::emoji_stuck_out_tongue:rice} of sunflower named "&e&lTrade Token" from {_p}
give {_p} {item{_n}}

command /openbuy <number>:
trigger:
set {i} to arg-1
buyItem(player, {i})

//This just doesnt work as expected, It was supposed to check if player has enough sunflowers and then remove the amount specified in the variable that the function will run for. I get the errror sunflower named "&e&lTrade Token" is not a world
 
function buyItem(p: player,n: number = 1):
if {_p}'s inventory contains {item::{_n}::emoji_stuck_out_tongue:rice} of sunflower named "&e&lTrade Token":
remove {item::{_n}::emoji_stuck_out_tongue:rice} of sunflower named "&e&lTrade Token" from {_p}
give {_p} {item{_n}}

command /openbuy <number>:
trigger:
set {i} to arg-1
buyItem(player, {i})

//This just doesnt work as expected, It was supposed to check if player has enough sunflowers and then remove the amount specified in the variable that the function will run for. I get the errror sunflower named "&e&lTrade Token" is not a world
One of the biggest issues I see is the invalid usage of variables. You do not need the majority of the "{item::{_n}::emoji_stuck_out_tongue:rice}" variable. Below I've rewritten your code, check the notes I made for reasons why I changed it.

Code:
function buyItem(p: player, n: number = 1, i: item):
    if {_p}'s inventory contains {_n} of sunflower named "&e&lTrade Token": # You just need to check for X (or {_n}) amount of the target item
        remove {_n} of sunflower named "&e&lTrade Token" from {_p} # Same as the previous line
        give {_p} 1 of {_i}  # Not sure what your original code intended, so I added this

command /openbuy <number>:
    trigger:
        set {i2} to diamond named "&bTest Item" # Sets a item you get for X sunflowers
        buyItem(player, arg-1, {i2}) # You can just put 'arg-1' instead of using a variable since arg-1 will always be a number