Meta accounts are here: you can create a Meta account to log into VR devices. Learn more

TRACK PLAYERS HIT

Now let’s use our score variable to track how many times a player has hit another player with a water balloon. The completed script is available at the bottom of the page.

First, open up the WaterBalloon script, and let’s start by keeping track of who threw the balloon. Let’s create a new variable called MyPlayer, and set it’s type to PlayerID. Then drag over the event, When Object is Released by Player, and from the Values tab, grab SetTo. Place the MyPlayer variable on the left, and bring the green Player pill down to the right. Perfect, we have now saved the PlayerID who threw the balloon!

Now, at the bottom of the When Colliding With Player event, from our Values tab, bring over Set Player Persistent Variable. From the drop down, select the score variable, and on the Operators tab, grab the plus symbol to replace the number value. From the Values tab, grab a number input for the “B” slot, and set it to one. Then at the top, grab Get Player Persistent Variable, and place it in the “A” slot. We can then select score from the drop down. And fill in the empty player slots with MyPlayer from the Variables tab. Now the player’s score increases by one every time the balloon collides with a player.

Sometimes the balloon will register hitting a player twice. We can create a boolean variable named OncePer that prevents double counting. When Object is Released by Player, bring over SetTo, placing OncePer on the left, and boolean value true on the right, then duplicate by selecting the whole SetTo codeblock, and thumbstick to the right in the When Colliding with Player event. We can then set the boolean value to false.

Bring an If into the Collision Event. Then from the Operators tab, grab And, placing OncePer on the left, and a not equals symbol on the right. We can then check that the player collision is not with MyPlayer. Dragging the player pill to the left, and the MyPlayer variable to the right. Then indent all of the Collision Event’s codeblocks, so they will only go off once.

You may remember that when we throw the water balloon at a wall, we also receive a Collision Event with Player, but the player value is Server Player. This should not increase the player’s score. To solve this, there is another great use of If, which is to nest the If. Bring If just above set player variable. Then duplicate the not equals codeblock. From values, replace MyPlayer with Server Player. And then, drag the Set Player Variable into the new If codeblock, allowing for wall splashes but preventing points for wall collisions.

To test our work, from the Values tab, grab Debug Print and place it inside the Collision Event’s If. Then, grab Variable as String, and duplicate the Get Player Variable codeblock from above. Now in our Build Menu on the Script tab, we can see that when a player is hit by a balloon, the score of the thrower is increased. Nice work! And in the next section we will create a scoreboard.

PREREQUISITE READING

PERSISTENT PLAYER VARIABLES precedes this tutorial