When players are in your game, they will want something to compete for. Developing something to compete for can take many forms. There could be tiers of upgrades, or cosmetic options they could buy or unlock. Instead, let’s create a leaderboard that can keep track of all player’s stats.
Setting Up the Leaderboard
The first step needed is to add a folder called leaderstats to players when they join. Naming the folder the leaderstats lets Studio know you want to create a leaderboard.
- Within ServerScriptService, open PlayerShipHandler.
- Create a new Folder when the player is added to the game that will store the leaderboard information.
- Under where you created the folder, create a new IntValue and parent it to the leaderstats folder created in the last step. The IntValue will store the number of enemies players have blasted.
- The IntValue’s name is what will be displayed to players. Change its name to
Scoreand set its starting value to
Now when players join the game, they’ll see a leaderboard on the right side showing that they haven’t earned any points yet.
In order to give players more points, the logic for
onDestroyEnemy() in the BlasterHandler script written earlier needs additional code.
- Within ServerScriptService, open BlasterHandler and find the
- After destroying the enemy, get the player’s stats. This is done by finding the IntValue named
Score, created earlier.
- Increase the value of
enemyStat, which stores the player’s score, by one.
Now players will be able to compete to score the highest as the game plays on…
Expanding onDestroyEnemy() further
The logic written for hitting enemies is very short. There’s a lot you can add to make your game unique. There are visual effects like particles and explosions. Or, make combat more exciting by adding sound effects and camera shakes.
Code Challenge: Make the Game Endless »
Many games feature an endless mode to the fun of the game. Using the tools learned in this series, expand the game to be endless.
(Hint: Use remote events to
spawnEnemies() when enemies are hit by the player’s lasers)
- Within ServerScriptService, open
spawnEnemiesuse a parameter for the spawn count.
- Change the call to
spawnEnemies()to use the constant variable.
- Create a constant variable.
- Create an
enemySpawnEventafter the other variables are defined.
- After the other functions are defined, add a function called
spawnEnemieswith a value of one or more. Adding more than 1 enemy per enemy hit may quickly make your game feel unfair - so exercise caution.
Finished Project Example
These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.