#how to use setItem when player is in a certain location and distance?

1 messages · Page 1 of 1 (latest)

real moth
#
world.getPlayers({location: new Vector3(x, y, z), maxDistance: r}).forEach(player =>{
     player.getComponent("inventory").container.setItem(0, new ItemStack("compass"))
})

replace "x", "y", "z" to coordinate and "r" to radius u want

#

Its 25 times shorter than u thought :))

#

@gentle helm Does it work?

minor bough
#

It’s just new Vector(), and imported from @minecraft/server

last tinsel
real moth
#

True

real moth
#

world.getAllPlayers() cant get EntityQueryOptions!!

#

Fix:

world.getPlayers({location: new Vector(x, y, z), maxDistance: r}).forEach(player =>{
     player.getComponent("inventory").container.setItem(0, new ItemStack("compass"))
})
#

Or
[...] location: {x: x, y: y, z: x}, [...]

minor bough
#

Have you tried specifying dimension as well?

#

It might be needed

amber solsticeBOT
#
Debug Result

There are 4 errors in this [code](#1190806530782679200 message):

<repl>.js:2:21 - error TS2554: Expected 0 arguments, but got 1.

2 world.getAllPlayers({location: new Vector(375, -59, 78), maxDistance: 140}).forEach(player =>{
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``````ansi
<repl>.js:2:36 - error TS2304: Cannot find name 'Vector'.

2 world.getAllPlayers({location: new Vector(375, -59, 78), maxDistance: 140}).forEach(player =>{
                                     ~~~~~~

``````ansi
<repl>.js:3:5 - error TS2532: Object is possibly 'undefined'.

3     player.getComponent("inventory").container.setItem(0, new ItemStack("compass"))
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``````ansi
<repl>.js:3:5 - error TS2532: Object is possibly 'undefined'.

3     player.getComponent("inventory").container.setItem(0, new ItemStack("compass"))
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

minor bough
#

And then have it in the getPlayers, yeah

void bough
#
world.getDimension('overworld').getPlayers({location: new Vector(375, -59, 78), maxDistance: 140}).forEach(player =>{
    player.getComponent("inventory").container.setItem(0, new ItemStack("minecraft:compass"))
})
real moth
#

This code use @gentle helm

system.runInterval(() => {
world.getDimension("overworld").getPlayers({location: {x: 0, y: 0, z: 0}, maxDistance: 1}).forEach(player =>{
     player.getComponent("inventory").container.setItem(0, new ItemStack("compass"))
})
}, 1)
#

Import it from "@minecraft/server"