How do you keep track of kills on the leaderboard in Rec Room?
I want to keep track of every elimination a player gets and store it in the leaderboard. Players should be able to see their total kills whenever they join the room and compare with others that play the game.
The answer to this depends on how you have your game setup: game rules chip health management, custom health management. In both cases, you need to use the player hit chip to detect when a player has been hit and then check to see if they have been eliminated, however, due to the player hit chip having a buffer of up to 20, you'll need to mitigate a common but easy-to-miss bug that will allow players to get multiple points off from a single elimination.
Duplicate Elimination Bug
A common pitfall with elimination counting is failing to account for the buffer in the player hit chip. For busy rooms, the player hit chip's buffer can be heavily saturated, and the slow 0.1s tick rate in Rec Room only exacerbates the problem. To understand the bug, picture the following scenario:
- A defender has just been struck by an attacker with all 5 projectiles of a burst rifle. These projectiles all hit the player within 1/10th of a second, causing the player hit chip to buffer up 5 hits on the defender.
- The defender has only 1 HP; low enough for a single projectile to have eliminated them.
- When the circuit processes the first hit, it sets their health to 0 and counts that as an elimination.
- Now that the defender is eliminated, no more hits can be registered, however... there are still 4 more hits buffered in the player hit chip that still haven't been processed.
- If the circuit does not check that they either have positive health first, or have a certain eliminated flag role, then each of the next 4 hits will give the attacker another elimination. They will gain 5 eliminations from what should have only been one elimination.
This will obviously ruin the validity of an elimination tracker, so it is important to mitigate it. Now that you understand this pitfall, read on to learn how to resolve it.
Custom Health Management
If you have custom health management, this ends up being pretty straightforward. In the your health management circuit, you should already know what the defender's health is before and after the hit. You should also have the attacker's ID. To avoid the pitfall mentioned above, execute the following steps using comparators:
- Check that the defender's health is positive before damage is applied.
- Check that the defender's health is less than or equal to 0 after damage is applied.
- If both of those are true, pass the attacker's ID into an incrementing leaderboard chip.
This system will be all you need to count eliminations with a custom health circuit.
Game Rules Health Management
If you're using the game rules chip to manage health, then you'll need to flag players that get eliminated to inform the circuit when they've already been counted as an elimination. You can either use a player stat or a role.
- Using the player hit chip, check if the health of the defender is 0.
- Check if the defender has the flag that you created (i.e. check if they have your custom eliminated role or that a designated player stat is not-equal to 0)
- If the flag is not present, then pass the ID of the attacker into an incrementing leaderboard chip, and then apply the flag to the defender (i.e. give them the role or set a designated player stat to 1).
- If the defender was just eliminated, make sure you pass their ID through a delay chip to remove the flag at least 2 seconds later. 2 seconds is long enough for the player hit chip's buffer to clear and will avoid any duplicate elimination bugs.
This system will be all you need to count eliminations when using the game rules chip to manage health.