Updated:
1. Setup (once)
/scoreboard objectives add coins dummy
/scoreboard objectives add roll dummy
2. Layout
An impulse command block with a button, set to Needs Redstone, followed by chain blocks set to Unconditional and Always Active. Put steps 3 to 6 in order.
3. Check emeralds
/execute as @p store result score @s coins run clear @s emerald 0
/execute as @p if entity @s[scores={coins=10..}] run tag @s add gacha
/execute as @p unless entity @s[tag=gacha] run tellraw @s {"text":"You need 10 emeralds","color":"red"}
/clear @a[tag=gacha] emerald 10
clear @s emerald 0 counts emeralds without removing any. Pick the player with as @p first, then test them, so a different player with emeralds is not chosen.
4. Roll 1 to 100
/execute as @a[tag=gacha] store result score @s roll run random value 1..100
See random command.
5. Prizes by range
/give @a[tag=gacha,scores={roll=1..5}] diamond 3
/give @a[tag=gacha,scores={roll=6..30}] iron_ingot 5
/give @a[tag=gacha,scores={roll=31..100}] bread 3
That is 5% jackpot, 25% win and 70% common. Change the ranges to change the odds, without gaps or overlaps.
6. Jackpot message and cleanup
/execute as @a[tag=gacha,scores={roll=1..5}] run tellraw @a [{"selector":"@s"},{"text":" hit the jackpot!","color":"gold"}]
/execute as @a[tag=gacha,scores={roll=1..5}] at @s run playsound minecraft:ui.toast.challenge_complete master @s
/tag @a[tag=gacha] remove gacha
Troubleshooting
- Says not enough emeralds: check the coins objective and that chains are Always Active.
- No prize: random needs 1.20.2 or newer.
- Bedrock Edition: use
scoreboard players random @p roll 1 100.
Check your command
The Command Error Doctor checks common syntax mistakes in your browser.