PT-BR EN ES
Channel Icon

Crazy Ozz

Official Channel Website

Command Block

How to Detect Nearby Mobs in Minecraft

Minecraft Java Edition 1.21+ and 26+ (Commands + Scoreboard)

In this tutorial, you will learn how to detect nearby mobs in Minecraft Java Edition 1.21+ and 26+ using a scoreboard and command blocks. This method is perfect for creating automatic alerts, custom map events, security systems, and triggers that activate when a mob is close to the player.

First, we are going to create a scoreboard that will work as a timer for the tellraw command we are going to run.

Here is the command to create the scoreboard below:

scoreboard objectives add mob_detection dummy

After that, place 4 command blocks in a row, all facing the same direction.

The first one should be a Repeat command block, and the next ones should be Chain command blocks.

Set all of them to Always Active.

And set all Chain command blocks to Conditional.

Here are the commands below, starting from the Repeat command block and going to the last Chain command block:

scoreboard players add timer mob_detection 1

execute if score timer mob_detection matches 20..

execute as @a at @s at @e[type=pig,distance=0..5] run tellraw @s {"text":"There is a pig nearby!","color":"green"}

scoreboard players set timer mob_detection 0

The idea is to create a timer that adds 1 point to the scoreboard every Minecraft tick.

Every 20 ticks equals 1 second, every 40 ticks equals 2 seconds, 60 = 3 seconds...

So, when we go past 20 ticks in the second command, the last 2 commands will be executed.

One command detects if there is a pig nearby within a distance of 5 blocks and notifies you using the tellraw command.

And the last command resets the scoreboard score, creating a loop.

Want cheap and lag-free Minecraft hosting?

I recommend Sparked Host: affordable plans, great performance, easy panel and fast support.

🚀 Start your Minecraft Server

Still have questions? Watch the video below