Skip to content

Marketplace

System · Marketplace · server and client scripts

Marketplace sells the products of your place for Lunes, the platform currency. You create products and set their prices on the website, a script shows the purchase prompt, and the server grants what the player bought.

PromptPurchase

Function
PromptPurchase(player: Entity, productId: number, productType: ProductType?) -> ()

PromptPurchase shows the purchase prompt for a product of this place to a player. Pass productType as "Consumable", "Durable", or "Subscription"; it defaults to "Consumable".

Effects

  • A request while a prompt is open, or within 2 seconds after the last one closed, opens nothing and fires PurchaseFinished with purchased set to false.
  • In Studio Play the prompt is a test purchase and spends no Lunes.

Example

-- Server script.
local VIP_PASS = 5678

-- offer the VIP pass to every player who joins
Component.User.Added:Connect(function(player: Entity)
    Marketplace.PromptPurchase(player, VIP_PASS, "Durable")
end)
Errors (2)
Error When
productId number must be an exact integer in [1, 2^53 - 1] (pass larger IDs as a string) A numeric productId was zero, negative, fractional, non-finite, or above 2^53 - 1.
productId string must be decimal digits in [1, 2^64 - 1] A string productId was empty, zero, contained non-digit characters, or exceeded 2^64 - 1.

OwnsProduct

Function
OwnsProduct(player: Entity, productId: number, options: MarketOptions?) -> (MarketStatus, boolean?)

OwnsProduct returns (status, owned), where status is "Ok", "TimedOut", or "Unavailable", and owned is whether a player owns a durable product or has an active subscription to a product of this place (nil when status is not "Ok"). A consumable is never owned.

Effects

  • Answers from what the server last fetched from Luduvo: on join, after each of the player's prompts closes, and when a subscription period ends. Right after a join a player owns nothing for a moment.
  • A modified client can fake its own products, so check ownership on the server.

Example

-- Server script in a door part.
local VIP_PASS = 5678

-- fade the door for players who own the VIP pass
self:On(Component.Collider.Touched, function(hit: Entity)
    local player = Players.Of(hit)
    if player then
        local status, owned = Marketplace.OwnsProduct(player, VIP_PASS)
        if status == "Ok" and owned then
            self.Color = Color.new(1, 1, 1, 0.5)
        end
    end
end)

GetSubscriptionStatus

Function
GetSubscriptionStatus(player: Entity, productId: number, options: MarketOptions?) -> (MarketStatus, SubscriptionStatus?)

GetSubscriptionStatus returns (status, sub), where status is "Ok", "TimedOut", or "Unavailable", and sub is a SubscriptionStatus table (nil when status is not "Ok"). sub.Active is false when the player has no subscription to the product.

Example

-- Server script in a gate part.
local SEASON_PASS = 9012
local seasonMembers: { [number]: boolean } = {}

-- mark season members when they touch the gate
self:On(Component.Collider.Touched, function(hit: Entity)
    local player = Players.Of(hit)
    if player then
        local status, sub = Marketplace.GetSubscriptionStatus(player, SEASON_PASS)
        local user = player.User
        if status == "Ok" and sub and user then
            seasonMembers[user.Id] = sub.Active
        end
    end
end)

HandlePurchase

FunctionServer only
HandlePurchase(productId: number, fn: (receipt: ProductReceipt) -> boolean) -> ()

HandlePurchase registers the server callback that grants a bought consumable for a product id. Pass the product id and a function that takes a ProductReceipt and returns true once the player has what they paid for.

Effects

  • Only a return of true marks the receipt delivered. Any other return, an error, or a yield such as task.wait leaves it undelivered, and the server passes it again later.
  • Runs for every undelivered receipt of a player when the player joins and after each of that player's prompts closes.

Example

-- Server script.
local COINS_PRODUCT = 1234
local coins: { [number]: number } = {}

-- give 100 coins for each coin pack bought
Marketplace.HandlePurchase(COINS_PRODUCT, function(receipt)
    local player = Players.Get(receipt.UserId)
    if player then
        coins[receipt.UserId] = (coins[receipt.UserId] or 0) + 100
        return true
    end
    return false
end)
Errors (1)
Error When
HandlePurchase can only be called from the server Called from a client script.

PurchaseFinished

EventRead only
PurchaseFinished: Signal<(player: Entity, productId: number, purchased: boolean) -> ()>

PurchaseFinished fires when a purchase prompt closes, passing the player, the product id, and whether the player bought the product.

Effects

  • Fires on both the client and the server. On a published server, purchased is true only after Luduvo confirms the purchase: the player owns the durable product or subscription, or the server receives a receipt for that consumable that no earlier true event used and that Luduvo created at most 30 seconds before the prompt closed. When Luduvo answers without the purchase, or does not answer within 10 seconds, the server event fires with false. In Studio, the server event fires at once.
  • The server event does not fire when the player leaves before Luduvo confirms the purchase.
  • A server script can grant a durable product or subscription when the server event passes true. Grant a consumable in HandlePurchase only, because that grant and a grant on purchased == true would give the player the reward twice.
  • A modified client can fake its own client event, so use the client event only to update the shop UI.

Example

-- Client script.
-- react when the local player closes a prompt without buying
Marketplace.PurchaseFinished:Connect(
    function(player: Entity, productId: number, purchased: boolean)
        if not purchased then
            print(player.Name, "closed the prompt for", productId)
        end
    end)