Skip to content

Build a shared score counter

This extends your first interaction into a small game system. It uses a Public score so different event calls can read the same state.

  1. Create a non-Final Public Number Variable named score.
  2. Add a Plot Start event.
  3. Follow it with Set Variable assigning score the Number 0.
Event: Plot Start
Set Variable: Public Number score ← 0

Plot Start initializes each program activation. If you want persistence instead, use a separate Save best score rather than making every current-round value persistent.

Add Player Right Click. Use Set Variable → Operations → Number → Add Numbers (+) with Result Public score, Left Operand Public score, and Right Operand Number 1. Then send a message on Event Target.

Event: Player Right Click
Set Variable → Add Numbers (+)
Result: Public Number score
Left Operand: Public Number score
Right Operand: Number 1
Call Method → Event Target → Send Message
Message: Shared score: ${public.score}

This intentionally uses a shared score: every player contributes to one total. It is not a per-player score just because a Player event changes it.

Compile, enter Play, and right-click. Expect the shared total to rise by one per matching event. Minecraft can generate interaction variants; if a game needs a specific held item or interaction context, add the appropriate item/event checks rather than assuming every right-click is the same interaction.

Declare non-Final Save Number bestScore. After incrementing, compare Public score against a Number expression ${save.bestScore ?? 0}. When greater, assign Save bestScore from Public score.

Send Score: ${public.score}; best: ${save.bestScore ?? 0} to display both. Compile and test zero, repeated increments, and reactivation: current score resets while best persists.