#Custom item that sets a score?

1 messages · Page 1 of 1 (latest)

solemn nebula
#

I have created an item, which is stackable up to 64 times. If I use this item, I want to run a function, that delets the current amount of this item, that the player is holding and sets a score that is eaqual to the amount of items, the player had in his hand. But I also only want to run the function, if the player is on said scoreboard. If he isn't, the whole function shouldn't run.

Has anybody an idea, how i would do this?

solemn nebula
#

I already got this:

#
import { world, system } from "@minecraft/server"

world.beforeEvents.itemUse.subscribe(data => {
    let player = data.source
    if (data.itemStack.typeId == "kap:mikrocoin") system.run(() => main(player))

    function main() {
        player.runCommand(`say HI`)
    }
})```
#

I just need the function to delete the amount of "kap:mikrocoin" the player is currently holding and to add this to his score

#

And to detect, if the player even is on the scoreboard, if he isn't I want the function not to trigger, if someone has an idea how this is done, please let me know☺️

worn crest
#
import { world, system } from "@minecraft/server"
 
world.beforeEvents.itemUse.subscribe(data => {

    const player = data.source;
    const item = data.itemStack;
    const amount = item.amount;

    if (item.typeId == "kap:mikrocoin") {

       console.warn(`Amount of items: ${amount}`);
       player.runCommandAsync(`scoreboard players add @s your_score_name ${amount}`);
       player.runCommandAsync(`clear @s kap:mikrocoin 0 ${amount}`);
     
   }
});```
worn crest
#

I just don't understand what you mean by "only if the player is on the scoreboard", wdym?

solemn nebula
#

So I made a "Bankaccount" scoreboard, the Player has to Do something to get activate his "Bankaccount" (get added to the scoreboard). If he has no account (isn't on the scoreboard) I want him to not be able to run the function

#

But until now, thank you so much😁

worn crest
#

what is the name of the score?

solemn nebula
#

The name of the scoreboard "money"

worn crest
# solemn nebula The name of the scoreboard "money"
import { world, system } from "@minecraft/server"
 
world.beforeEvents.itemUse.subscribe(data => {

    const player = data.source;
    const item = data.itemStack;
    const amount = item.amount;

    const score = world.scoreboard.getObjective("money").getScore(player);

   if (score < 1) {
  return;
}

    if (item.typeId == "kap:mikrocoin") {

       console.warn(`Amount of items: ${amount}`);
       player.runCommandAsync(`scoreboard players add @s your_score_name ${amount}`);
       player.runCommandAsync(`clear @s kap:mikrocoin 0 ${amount}`);
     
   }
});```
worn crest
solemn nebula
#

While I am at it might as well ask:
What is the difference between .runcommand and .runcommandAsync?

#

And thank you very much😁

worn crest
solemn nebula
#

I think that clears the hole inventory of the player right? If I just wanted to clear the items, the player has in his hand, do I need to add something 😅

Ohhh, this was the side I searched for

worn crest
#

discord doesn't notify me in notifications

#

hmmmm, you can use replaceitem ig

#
import { world, system } from "@minecraft/server"
 
world.beforeEvents.itemUse.subscribe(data => {

    const player = data.source;
    const item = data.itemStack;
    const amount = item.amount;

    const score = world.scoreboard.getObjective("money").getScore(player);

   if (score < 1) {
  return;
}

    if (item.typeId == "kap:mikrocoin") {

       console.warn(`Amount of items: ${amount}`);
       player.runCommandAsync(`scoreboard players add @s your_score_name ${amount}`);
       player.runCommandAsync(`replaceitem entity @s slot.weapon.mainhand 0 air`);
     
   }
});```
solemn nebula
#

I will test it soon, thank you so much 😁

solemn nebula
#

Short Feedback:

#

The code is perfectly working, I don't even need the "if (score < 1) {return;})", to identify if the player is on the scoreboard. If he isn't the code fails to run, because it can not get the "world.scoreboard.getObjective("money").getScore(player)"

#

So my final solution looks like this: