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!