question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Finding nearset players

See original GitHub issue

I’m trying to make a bot that attacks nearby players. The problem is that the bot only tracks the first nearest player, and if the player leaves the server or teleports to another world, the bot simply remembers the last point of coordinates. How to make sure that if a player disconnects from the server, the bot selects a new closest player?

if (message == ".farm") {
  clearInterval(afk)
  var target = minecraft.nearestEntity(({
    type
  }) => type === 'player')
  minecraft.pathfinder.setMovements(defaultMove)
  minecraft.pathfinder.setGoal(new GoalFollow(target, 1), true)
  setInterval(() => {
    let distance
    if (target.position) {
      let distance = target.position.distanceTo(minecraft.entity.position)
    }
    if (distance < 3) {
      minecraft._client.write('arm_animation', {
        hand: 0
      });
      minecraft.setQuickBarSlot(1)
      minecraft.attack(target)
    }
  }, 200)
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TheDudeFromCIcommented, Oct 8, 2020

This is because you forgot to check the target in the event.

bot.on('entityGone', (entity) => {
  if (entity !== target) return // <======== This Line ==========
  attackNearestPlayer()
})

Without that line, the bot will select a new target any time any entity despawns, dies, teleports, gets unloaded, moves out of render distance, etc. Which is quite often.

Also, I recommend you move that target variable to a global scope so it can be read by both functions.

Lastly, don’t call bot.on inside a command, since this creates a new listener each time, but does not remove the existing ones. You’ll create a memory leak and also have the same code running multiple times in parallel which can cause unexpected bugs later on. It’s best to create that method right after creating the bot. With the if (entity !== target) return line, this means that if not target is assigned, it will never be called. If a target is assigned, it will only be called when that target specifically disappears.

0reactions
shketovcommented, Oct 9, 2020

This is because you forgot to check the target in the event.

bot.on('entityGone', (entity) => {
  if (entity !== target) return // <======== This Line ==========
  attackNearestPlayer()
})

Without that line, the bot will select a new target any time any entity despawns, dies, teleports, gets unloaded, moves out of render distance, etc. Which is quite often.

Also, I recommend you move that target variable to a global scope so it can be read by both functions.

Lastly, don’t call bot.on inside a command, since this creates a new listener each time, but does not remove the existing ones. You’ll create a memory leak and also have the same code running multiple times in parallel which can cause unexpected bugs later on. It’s best to create that method right after creating the bot. With the if (entity !== target) return line, this means that if not target is assigned, it will never be called. If a target is assigned, it will only be called when that target specifically disappears.

Oh, my bad. Thank you so much, everything is working as it should!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Find the nearest player - Scripting Support - DevForum | Roblox
Find the nearest player · Check if their character exists and if it doesn't, ignore them · Check if their distance from the...
Read more >
How do i find the nearest player? - Scripting Helpers
Get all players and their characters. Iterate through all characters and check which root part is closest. I wrote this all in StackEdit...
Read more >
Get the nearest player | Bukkit Forums
Afaik there's no method for getting the nearest player, you'd have to do some maths. List ents = event.getPlayer().getLocation().getWorld().
Read more >
1.16.5 - Finding nearest player to another player - SpigotMC
To add up to this, you can avoid checking by simply getting all entities that are players in the specific world of your...
Read more >
Closest Player Detection Algorithm - 101 Computing
To find out who the closest player is to the ball, we need to calculate the distance between the ball and each player...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found