Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Prompt to buy a gamepass?

Asked by 5 years ago

I have looked all over and nowhere have I found a solution, I even tried just copy and pasting from the new wiki site and altering the gamepass ID. I tried changing the ID ot the full link. I have no idea where to go from here. Reading the new way to handle gamepasses on the devforum does not address this anywhere though? The ID is always leading me to an asset not the gamepass.

-- This code should be within a 'LocalScript' object
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local gamePassID = 5327756  -- Change this to your game pass ID

-- Function to prompt purchase of the game pass
local function promptPurchase()

    local player = Players.LocalPlayer
    local hasPass = false

    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
    end)

    if not success then
        warn("Error while checking if player has pass: " .. tostring(message))
        return
    end

    if hasPass == true then
        -- Player already owns the game pass; tell them somehow
    else
        -- Player does NOT own the game pass; prompt them to purchase
        MarketplaceService:PromptGamePassPurchase(player, gamePassID)
    end
end

Anyone know why?

1
Do you have FE enabled? Mpire_z -2 — 5y
1
^ Isn’t FE forced on for every game? User#20279 0 — 5y
1
Also, the code is in a function. I don’t see anything in the code that is calling it, so I’m assuming the function won’t run at all. Try adding “promptPurchase()” below and outside the function. User#20279 0 — 5y

1 answer

Log in to vote
0
Answered by
OfcPedroo 396 Moderation Voter
5 years ago

AFAIK, the code is well written, without any errors.. Actually, I think I've seen it before, I believe that's in a template. So, first of all, I advice you to check to see if that gamepass ID actually exists, and it is one that you created (5327756).

Assuming you did, you turned the code into a function, but never called it. So:

We can make it run the function:

-- This code should be within a 'LocalScript' object
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local gamePassID = 5327756  -- Change this to your game pass ID

-- Function to prompt purchase of the game pass
local function promptPurchase()

    local player = Players.LocalPlayer
    local hasPass = false

    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
    end)

    if not success then
        warn("Error while checking if player has pass: " .. tostring(message))
        return
    end

    if hasPass == true then
        -- Player already owns the game pass; tell them somehow
    else
        -- Player does NOT own the game pass; prompt them to purchase
        MarketplaceService:PromptGamePassPurchase(player, gamePassID)
    end
end

-- Run the function
promptPurchase()

or make it actual running code, or by other words, remove the function indication:

-- This code should be within a 'LocalScript' object
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local gamePassID = 5327756  -- Change this to your game pass ID

-- Make this not be a function, so it will run.
local player = Players.LocalPlayer
local hasPass = false

local success, message = pcall(function()
    hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
end)

if not success then
    warn("Error while checking if player has pass: " .. tostring(message))
    return
end

if hasPass == true then
    -- Player already owns the game pass; tell them somehow
else
    -- Player does NOT own the game pass; prompt them to purchase
    MarketplaceService:PromptGamePassPurchase(player, gamePassID)
end

If it works, please mark this as the correct answer. :P

0
Yes I gave up on doing it myself and literally took it right out of the Wiki it is the exact template from the wiki lol. I will try this when I get home, I seem to have overlooked this thank you CrispyBrix 113 — 5y
0
Alright. When you test it, simply let me know something, and if it works, please mark as solved. :P OfcPedroo 396 — 5y
Ad

Answer this question