PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Prerequisites Have completed Arcade Action - Ship Blasters
Lesson Time 5 - 10 minutes
Optional Handouts N/A
Learning Objectives
  • Create a leaderboard that uses an IntValue to display a player's score
  • Expand on previous code to add to a player's score whenever they blast an enemy
  • 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.

    1. Within ServerScriptService, open PlayerShipHandler.
    2. Create a new Folder when the player is added to the game that will store the leaderboard information.
    local function onPlayerAdded(player)
    	player.CharacterAdded:Connect(onCharacterAdded)
    
    	-- Add leaderboard functionality for each player
    	local leaderstats = Instance.new("Folder", player)
    	leaderstats.Name = "leaderstats"
    end
    1. 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.
    local function onPlayerAdded(player)
    	player.CharacterAdded:Connect(onCharacterAdded)
     
    	local leaderstats = Instance.new("Folder", player)
    	leaderstats.Name = "leaderstats"
    	
    	-- Add destroyed enemy count
    	local enemyCount = Instance.new("IntValue", leaderstats)
    end)
    1. The IntValue’s name is what will be displayed to players. Change its name to Score and set its starting value to 0.
    local function onPlayerAdded(player)
    	player.CharacterAdded:Connect(onCharacterAdded)
    
    	-- Add leaderboard functionality for each player
    	local leaderstats = Instance.new("Folder", player)
    	leaderstats.Name = "leaderstats"
    
    	-- Add destroyed enemy count
    	local enemyCount = Instance.new("IntValue", leaderstats)
    	enemyCount.Name = "Score"
    	enemyCount.Value = 0
    
    end)

    Now when players join the game, they’ll see a leaderboard on the right side showing that they haven’t earned any points yet.

    leaderboard

    Expanding onDestroyEnemy

    In order to give players more points, the logic for onDestroyEnemy() in the BlasterHandler script written earlier needs additional code.

    1. Within ServerScriptService, open BlasterHandler and find the onDestroyEnemy() function.
    2. After destroying the enemy, get the player’s stats. This is done by finding the IntValue named Score, created earlier.
    -- Called when the server gets an event saying an enemy was hit
    local function onDestroyEnemy(player, enemy)
    	enemy:Destroy()
    	local enemyStat = player.leaderstats:WaitForChild("Score")
    end
    1. Increase the value of enemyStat, which stores the player’s score, by one.
    -- Called when the server gets an event saying an enemy was hit
    local function onDestroyEnemy(player, enemy)
    	enemy:Destroy()
    	local enemyStat = player.leaderstats:WaitForChild("Score")
    	enemyStat.Value = enemyStat.Value + 1
    end

    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.

    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)

    1. Within ServerScriptService, open EnemyHandler
    2. Make spawnEnemies use a parameter for the spawn count.
    local function spawnEnemies(count)
    	local baseplateSize = workspace.Arena.Baseplate.Size
    
    	-- Loop until we have as many enemy as we want
    	for i = 1, count do
    1. Change the call to spawnEnemies() to use the constant variable.
    -- Have our enemies spawn
    spawnEnemies(ENEMY_COUNT)
    1. Create a constant variable.
    2. Create an enemySpawnEvent after the other variables are defined.
    local enemySpawnEvent = Instance.new("RemoteEvent", ReplicatedStorage)
    enemySpawnEvent.Name = "EnemySpawnEvent"
    
    1. After the other functions are defined, add a function called onEnemySpawnEvent()
    local function onEnemySpawnEvent()
    
    end
    
    1. Call spawnEnemies with a value of one or more. Adding more than 1 enemy per enemy hit may quickly make your game feel unfair - so exercise caution.
    local function onEnemySpawnEvent()
    	spawnEnemies(1)
    end
    

    Finished Project Example

    With the player leader boards added, the core gameplay of the arcade action game is finished. You can download a final project example here if needed.



    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.


    Previous Ship Blasters Next Earning Robux Through Games