We use cookies on this site to enhance your user experience

Monetization

Monetization

Prerequisites
  • Students should have completed Arcade Action - Player Stats
  • Students should have learned how to create a Game Pass
  • Lesson Time 5 - 10 minutes
    Optional Handouts N/A
    Learning Objectives
  • Learn how to use game passes to create an in-game purchase for the Arcade Game
  • Create a script that provides players a fire effect if they've purchased a game pass
  • Players of all ages love to show off how cool their character looks in a game. For this top down game, let’s give players a particle effect if they purchase a Game Pass. To learn how to create a GamePass, follow this tutorial found on the Devhub. The code below will use the value you give gamePassId to add a huge fireball to their ship while they play.

    Adding HasGamePassChecker

    HasGamepassChecker will check if players already have the game pass you’ve created using a ModuleScript.

    1. In ReplicatedStorage, create a new ModuleScript.
    2. Rename the ModuleScript HasGamepassChecker.
    3. Copy this code in the code sample below to this new ModuleScript:
    local HasGamepassChecker = {}
    
    local MarketplaceService = game:GetService("MarketplaceService")
    local Players = game:GetService("Players")
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local RunService = game:GetService("RunService")
    
    local MARKETPLACE_RETRIES = 5
    local RETRY_DELAY = 0.5
    local RETRY_FUNCTION_NAME = "GamepassRetryFunction"
    
    -- Setup the RemoteFunction to communicate between server scripts and
    -- any client it needs to send notifications to
    local retryRequest
    if RunService:IsServer() then
    	retryRequest = ReplicatedStorage:FindFirstChild(RETRY_FUNCTION_NAME)
    	if not retryRequest then
    		retryRequest = Instance.new("RemoteFunction")
    		retryRequest.Name = RETRY_FUNCTION_NAME
    		retryRequest.Parent = ReplicatedStorage
    	end
    else
    	retryRequest = ReplicatedStorage:WaitForChild(RETRY_FUNCTION_NAME)
    end
    HasGamepassChecker.retryRequest = retryRequest
    
    local function getPlayerHasPassRetry(playerId, passId)
    	local tries = 0
    	local success = true
    	local hasPass = nil
    	repeat
    		tries = tries + 1
    		success = pcall(function()
    			hasPass = MarketplaceService:UserOwnsGamePassAsync(playerId, passId)
    		end)
    	until success or tries == MARKETPLACE_RETRIES
    	if not success then
    		error("ERROR: Problem checking if player owns gamepass")
    	end
    	return hasPass
    end
    
    function HasGamepassChecker:getPlayerHasPass(playerId, passId)
    	assert(RunService:IsServer())
    	local success, hasPass, retry = false, false, false
    	local player = Players:GetPlayerByUserId(playerId)
    	repeat
    		success, hasPass = pcall(getPlayerHasPassRetry, playerId, passId)
    		if not success then
    			retry = retryRequest:InvokeClient(player, "Can't connect to server", "Purchases may not show correctly", "Try Again")
    		end
    	until not retry
    	
    	return success and hasPass
    end
    
    
    return HasGamepassChecker
    

    Adding the Effect to the Game

    1. Go to ServerScriptService, and open PlayerShipHandler
    2. Add the following variables to the script:
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    Local HasGamepassChecker = require(ReplicatedStorage:FindFirstChild("HasGamepassChecker")
    
    -- Unique number of the GamePass you created
    local GAMEPASS_ID = 0000000 -- Replace this with your GamePass’s ID
    -- Determines the heat (how fast the flame moves) and size
    local FIRE_HEAT = 20
    local FIRE_SIZE = 30
    
    1. Add a function above onCharacterAdded() called gamePassVerification() with a parameter named player that will store the player.
    -- Called to check if a joining player has bought the GamePass with the id from gamePassId
    local function gamePassVerification(player)
    
    end
    
    1. Call HasGamepassChecker’s getPlayerHasPass() function to see if the player owns the pass.
    local function gamePassVerification(player)
    	if HasGamepassChecker:getPlayerHasPass(player.UserId, GAMEPASS_ID) then
    	end
    end
    
    1. Under the if statement, spawn a Fire object and set it’s heat and size.
    local function gamePassVerification(player)
    
    	if HasGamepassChecker:getPlayerHasPass(player.UserId, GAMEPASS_ID) then
    		local fireEffect = Instance.new("Fire", player.Character.HumanoidRootPart)
    		fireEffect.Heat = FIRE_HEAT
    		fireEffect.Size = FIRE_SIZE
    	end
    end
    
    1. Add an else statement, printing to the console that there was an error.
    local function gamePassVerification(player)
    	if HasGamepassChecker:getPlayerHasPass(player.UserId, GAMEPASS_ID) then
    		local fireEffect = Instance.new("Fire", player.Character.HumanoidRootPart)
    		fireEffect.Heat = FIRE_HEAT
    		fireEffect.Size = FIRE_SIZE
    	else
    		print("player either doesn't have pass or there was an error and player didn't want to retry")
    	end	
    end
    

    The last step above can be changed to do other unique things instead. For example, you can create a Trail effect or a Particle Emitter instead of a Fire.


    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.