Finding nearset players
See original GitHub issueI’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:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is because you forgot to check the target in the event.
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 theif (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!