#Query for player position from script

1 messages · Page 1 of 1 (latest)

old goblet
#

Trying to summon tnt at a player's feet when they eat a specific food.
I'm having trouble with the query.position syntax for the player and im not sure it's even applicable in this situation

Current code in foodSpawnTnt.js

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

world.afterEvents.itemCompleteUse.subscribe(event => {
    if (event.itemStack.typeId === "minecraft:bread") {
        const p = event.source;
        const d = p.dimension;
        (async () => {
            //This is to test functionality
            await d.runCommandAsync("effect " + p.name + " invisibility 1 1 true");
            //I want to query the player's position and replace the ~ ~ ~ with that attained position
            await d.runCommandAsync("summon tnt ~ ~ ~")
        })();
        world.sendMessage("food eaten");
    }
});
plucky holly
#

I dont really got an idea on how to fix this

#

but id use bing to fix it

#

wait give me a minute

#

ima try fixing it 4 u

#

I think i know what you di wrong

#

try this "import { world, system } from "@minecraft/server";

world.afterEvents.itemCompleteUse.subscribe(event => {
if (event.itemStack.typeId === "minecraft:bread") {
const p = event.source;
const d = p.dimension;
(async () => {
// This is to test functionality
await d.runCommandAsync(effect ${p.name} invisibility 1 1 true);
// I want to query the player's position and replace the ~ ~ ~ with that attained position
const playerPosition = await d.runCommandAsync(execute in ${p.name} run tp ${p.name} ~ ~ ~);
const [x, y, z] = playerPosition.split(" ");
await d.runCommandAsync(summon tnt ${x} ${y} ${z});
})();
world.sendMessage("food eaten");
}
});
"

#

and make sure lines are right

#

these where issues

#

Its caz your backticks werent allowing command interpreation

#

@old goblet

#

I hope i helped you well

analog sage
#

also check out Player.addEffect and Dimension.spawnEntity

old goblet
# plucky holly try this "import { world, system } from "@minecraft/server"; world.afterEvents....

This threw an error
[Scripting][error]-Plugin [ScriptTest - 1.0.0] - [eatSpawnTnt.js] ran with error:[SyntaxError: expecting ',' at eatSpawnTnt.js:12]

The script:

    world,
    system
} from "@minecraft/server";

world.afterEvents.itemCompleteUse.subscribe(event => {
    if (event.itemStack.typeId === "minecraft:bread") {
        const p = event.source;
        const d = p.dimension;
        (async () => {
            await d.runCommandAsync(effect ${p.name} invisibility 1 1 true);
            const playerPosition = await d.runCommandAsync(execute in ${p.name} run tp ${p.name} ~ ~ ~);
            const [x, y, z] = playerPosition.split(" ");
            await d.runCommandAsync(summon tnt ${x} ${y} ${z});
        })();
        world.sendMessage("food eaten");
    }
});```
old goblet
old goblet
#

For anyone who comes across this, the final, working, code:

    world,
    system
} from "@minecraft/server";

world.afterEvents.itemCompleteUse.subscribe(event => {
    if (event.itemStack.typeId === "minecraft:bread") {
        const p = event.source;
        const d = p.dimension;
        //Makes player temporarily invisible
        p.addEffect("invisibility",40,{amplifier:1,showParticles:true})
        //Summons tnt on player who ate the food
        d.spawnEntity("minecraft:tnt", p.location)
    }
});```
fast dew
old goblet
#

considering I cobbled this together from some basic python knowledge and staying up till' midnight scouring forum posts on this stuff, I don't really know what im doing with JS

#

is it even necessary to use the async at all here?

old goblet
#

epic

old goblet
novel apex
dusky peak
#

Learn some basics @novel apex