1

I am making a minigame where you are given 15 blocks to bridge and I want to make the placed blocks disappear after about 5 seconds after being placed, as of right now I have no clue how to do this.

I don't think there is an age system for the blocks similar to items where they despawn after 5 minutes.

1 Answer 1

0

You're right, there is no built-in block that would vanish by itself over time, so you'll need to create your own system.

Step 1: Know where the block was placed

There are many ways to know where a block was placed, the most common one being raycasting. This one is not without its faults however, including not being 100% reliable and requiring some advanced commands / the usage of datapacks/functions for it to work properly.

So I recommend in your specific situation (highly controlled environment, single type of block) to use a different approach: Give the players (re-textured) spawn eggs instead of blocks. Let these spawn eggs then summon a marker entity with special tags that you can detect to place the block manually. Here is an example using a bat spawn egg item and a marker with the new tag.

# the give command
/give @p bat_spawn_egg{EntityTag:{id:"minecraft:marker",Tags:["new"]}} 15
# the system to detect the new marker and place the blocks
# run this on repeat
/execute at @e[type=marker,tag=new] run setblock ~ ~ ~ stone

Step 2: Remove the block after a while

Now that we have the block AND a marker at its position, we can use that marker to count the time it has existed, before we remove it again.

For that, we first need a dummy timer scoreboard.

# run once in chat
/scoreboard objectives add timer dummy

Then we can set all new markers scores to the amount of time we want the block to stay around in ticks. 5 seconds = 100 ticks. We count down the score of all markers, and when they reach 0 we remove the block and the marker.

# run these on repeat, in a chain
# preferably in the same chain as the execute->setblock command above to make sure
# they execute in the correct order, which is important
/scoreboard players set @e[type=marker,tag=new] timer 100
/tag @e[type=marker,tag=new] remove new
/scoreboard players remove @e[type=marker,scores={timer=1..}] timer 1
/execute at @e[type=marker,scores={timer=..0}] run setblock ~ ~ ~ air
/kill @e[type=marker,scores={timer=..0}]

And there you have it, that's how you do that.


The nice thing about this system is that it's so easily modifiable, basically invisible to the players (if you change the spawn egg texture/model) and needs barely any commands. Want the block to stay around for a different time? Change the score that is given to the marker. Want the block to change over time? Just check for different scores of the marker and set the block accordingly.

2
  • It works, but is there a way to do this using ray casting so It is possible to use blocks instead of spawn eggs
    – CealDP
    Feb 16 at 10:46
  • As mentioned in the post, yes there is. But I suggest you use a resourcepack to remodel/retexture the spawnegg instead of using a raycast. This system is orders of magnitude simpler than setting up a (raycast based) system that figures out with 100% certainty where a player just placed a block. I've linked an explanation page on raycasting in my answer, you can have a look at that if you want a raycast based method. Combine it with a custom advancement using the placed_block trigger and you've got a solution.
    – Plagiatus
    Feb 16 at 12:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .