How to destroy a block and give you an item in your inventory at the same time?
So me and my friend are creating a Mario-like game in Minecraft Bedrock Edition. We are struggling with trying to come up with code. What we need is so if the player is standing on a red mushroom it breaks the mushroom and gives the player an item. The problem is that we need to have both things happen at the exact same time (without a comparator delay too) so that you can pick up multiple mushrooms in a row without missing any.
We have tried
/execute @a ~~~ detect ~~~ red_mushroom 0 fill ~~~ ~~~ air
and then hooking it up to a give command using a comparator. Obviously it works but we cant have a delay between picking up the mushrooms.
We have also tried running two execute commands in repeating, one that gives you an item and one that destroys the mushroom. The problem is it creates a 50/50 of if it works or not.
Could someone help us out with this?
There is a simple fix to this:
Have a Repeat command block set to Always Active, with this command:
/execute @a ~~~ detect ~~~ red_mushroom 0 fill ~~~ ~~~ air
There is a house/pentagon-shaped object with dots on it, and it should be on top, and pointing to a chain command block.
The chain command block should have your command that gives the player a mushroom, set to Always Active, and Conditional. The Conditional statement is there for this reason:
If the Repeat command block detects a player on a mushroom, then they will give that player a mushroom. If not, then nothing happens. If the block was Unconditional, then players would have their inventories cluttered with mushrooms. I hope this helps.
Edit: Because we want to detect which player stepped on a mushroom, we need separate pairs of command blocks for each player, like so:
/execute fasterthanlight ~~~ detect ~~~ red_mushroom 0 fill ~~~ ~~~ air
/give fasterthanlight red_mushroom 1
For each player, set up a different pair of command blocks with these commands, and replace my username with your targeted player. For example, with two players, Alice and Bob, two pairs of blocks will be used:
/execute Alice ~~~ detect ~~~ red_mushroom 0 fill ~~~ ~~~ air
/give Alice red_mushroom 1
and
/execute Bob ~~~ detect ~~~ red_mushroom 0 fill ~~~ ~~~ air
/give Bob red_mushroom 1
Here is an answer that works no matter how many players there are.
Place down an always active repeating command chain of 2 blocks.
Commands inside:
-
Tell everyone with a
red_mushroom
under them to give themselves an item./execute @a ~ ~ ~ detect ~ ~-1 ~ red_mushroom 0 give @s mushroom
-
Tell everyone with with a
red_mushroom
under them to set the block underneath them to air./execute @a ~ ~ ~ detect ~ ~-1 ~ red_mushroom 0 setblock ~ ~-1 ~ air