#Execute code if no other player has tag

1 messages · Page 1 of 1 (latest)

proven axle
#

I am wanting to run code, only if there is no player nearby with a specific tag.

here is my code:

import { system, world } from "@minecraft/server";
system.runInterval(() => {
  // Get players to run script from
  for (const player of world.getPlayers()) {
    // Check if player has no tag
    if (!player.hasTag("test_tag")) {
      const otherPlayer = player.dimension.getPlayers({
        excludeNames: [player.name],
        maxDistance: 12,
      });
      otherPlayer.forEach((oPlayer) => {
        if (oPlayer.hasTag("test_tag")) {
          return;
        }
      });

/// ONLY RUN THIS IF NO PLAYERS WITH "test_tag" NEARBY
      const entities = player.dimension.getEntities({
        location: {
          x: player.location.x,
          y: player.location.y,
          z: player.location.z,
        },
        maxDistance: 4,
      });
      // for each entity, reset name
      entities.forEach((entity) => {
        if (entity.hasTag("notnamed")) {
          entity.nameTag = ``;
          entity.removeTag("notnamed");
        }
        if (entity.hasTag("named")) {
          const lines = entity.nameTag.split("\n");
          const exeName = lines[0];
          entity.nameTag = `${exeName}`;
          entity.removeTag("named");
        }
      });
    }
  }
});

I have tried a few methods to filter nearby players and then have the code run, but I am unsure how to do it.

Any help would be amazing!

chilly creek
#

try this

#
system.runInterval(() => {
  world.getPlayers().forEach(player => {
    if (player.hasTag("test_tag")) return
    
    const otherPlayer = player.dimension.getPlayers({
      maxDistance: 12
    }).filter(f => !f.hasTag("test_tag") && f.name !== player.name)
    
    if (!otherPlayer) return
    
    //your code
    console.log(otherPlayer[0].name)
  })
})