Hi There!
Welcome back to my marketplace tutorial. Did you know you can use PromptGamePassPurchase()
with module scripts? Well I’m gonna teach you how to do that in this tutorial.
DISCLAIMER!
I’m not a professional at module scripts so if I mess up, please correct my mistakes in this topic.
1. Setting up the module
Let’s get started by adding a module script in ReplicatedStorage.
In the module script, start off by naming our module (You can name it whatever you want).
local MarketplaceFunctions = {}
Now we will start off with the function for our module that will prompt the game pass. Write the function like this:
function MarketplaceFunctions.prompt(service, player, ID)
-- Do stuff
end
service
is the MarketplaceService. It will be fired in a separate script. So if we’re gonna do the prompt, just write the function like this:
function MarketplaceFunctions.prompt(service, player, ID)
service:PromptGamePassPurchase(player, ID)
end
And ALWAYS remember to add the return
section, otherwise this is the error you will get:
Returning values is important because if you don’t use return
in your module script, it will mess up the entire code you’re firing it in.
So in the end, your module script will look like this:
local MarketplaceFunctions = {}
function MarketplaceFunctions.prompt(service, player, ID)
service:PromptGamePassPurchase(player, ID)
end
return MarketplaceFunctions
2. Firing the module
Now we’re gonna test out our module. We can use many different ways to do it but for now, let’s just use GUIs.
First, add a local script inside your screen gui, followed by a text button.
Then in your local script, we will request our module.
local marketplaceFunction = require(game.ReplicatedStorage.ModuleScript)
Next, we assign our main variable followed by our local player.
local mps = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer -- That's you! :)
So here’s how we do this part: We’ll do a MouseButton1Click
function:
script.Parent.TextButton.MouseButton1Click:Connect(function()
-- Do stuff
end)
Then insert our marketplace function we requested inside the function.
script.Parent.TextButton.MouseButton1Click:Connect(function()
marketplaceFunction.prompt(mps, player, 0) -- Replace the zero with your gamepass ID.
end)
There you have it!
It should all look like this in the end:
Our module script:
local MarketplaceFunctions = {}
function MarketplaceFunctions.prompt(service, player, ID)
service:PromptGamePassPurchase(player, ID)
end
return MarketplaceFunctions
Our local script:
local player = game.Players.LocalPlayer
local mps = game:GetService("MarketplaceService")
local marketplaceFunction = require(game.ReplicatedStorage.ModuleScript)
script.Parent.TextButton.MouseButton1Click:Connect(function()
marketplaceFunction.prompt(mps, player, 0)
end)
Thanks for Reading!
Like I said, in case I miss out or mess up anything in the topic, feel free to mention it to me.