How should I test for when 2 players have a total score of 20 when added together?
I need to kill players when together they have obtained 20 points in total.
Say 1 player had 10 and the other had 10. When I add them together they add up to 20 and they need to be killed. How should I perform checks and execute /kill
if necessary, using commands?
Solution 1:
Answer
scoreboard players operation #sum myScoreboard = @a[tag=player1] myScoreboard
scoreboard players operation #sum myScoreboard += @a[tag=player2] myScoreboard
execute if score #sum myScoreboard matches 20 run say Player 1 and Player 2 have a total score of 20
Explanation
Line 1:
scoreboard players operation #sum myScoreboard = @a[tag=player1] myScoreboard
Line 1 copies the score of Player 1 to another container. To do this, a fake player, #sum
, is used. The objective this fake player belongs to does not matter.
Using the scoreboard players operation
command, #sum
's score is set equal to Player 1's score using the =
operator.
Player 1 is referenced by the target selector, @a[tag=player1]
. Research target selectors for more information.
Line 2:
scoreboard players operation #sum myScoreboard += @a[tag=player2] myScoreboard
Line 2 adds the score of Player 2 to #sum
. Similarly to line 1, scoreboard players operation
is utilized, except this time, the +=
operator is used instead of =
.
Line 3:
execute if score #sum myScoreboard matches 20 run say Player 1 and Player 2 have a total score of 20
Line 3 tests if the score of #sum
matches 20. If it passes, "Player 1 and Player 2 have a total score of 20" will be displayed in chat.