#how to catch error and send message

1 messages · Page 1 of 1 (latest)

primal canopy
#

how to catch this error and send a message to the world
[Scripting][error]-Error: Invalid item identifier 'mba:item_head'

const ind = "mba:item_head_" + hex_name
const head = new mc.ItemStack(ind, 1);
player.dimension.spawnItem(head, player.location);
viral gate
#

Wrap in trycatch

primal canopy
#

not familiar with that
do you have an example?

#

like this
try {
//code
} catch ( error ) {//send message}

alpine talon
#

Your error shows the name of the identifier without the underscore at the end or the appending hex_name? Are you certain that the block of code you shared is where the error is stemming from?

primal canopy
# alpine talon Your error shows the name of the identifier without the underscore at the end or...

yeah
i am making killed players drop there heads but if they don't have one the error shows up
any way i fixed that

world.afterEvents.entityDie.subscribe(event => {
    const player = event.deadEntity
    if (player.typeId != "minecraft:player") return;
    const name = player.nameTag
    let hex_name = '';
    for (let i = 0; i < name.length; i++){hex_name += name.charCodeAt(i).toString(16)}
    try {
        const ind = "mba:item_head_" + hex_name
        const head = new mc.ItemStack(ind, 1);
        player.dimension.spawnItem(head, player.location);
    } catch ( error ) {
        world.sendMessage(`§c${name}\'s head dosn\'t exist in the pack`)
    }
});
alpine talon
primal canopy
#

no i deleted it so i can test staff : )

alpine talon
#

If it doesn't exist then you can't spawn it.

primal canopy
#

yeah that's why i need to catch the error and send a message that the player head dosn't exist in the pack

alpine talon
#

Ah, I see. Well the block of code above has a try/catch wrapped around it so that should do the job.

primal canopy
#

yeah i said that i fixed that

alpine talon
#

Didn't see that