Introduction: We are going to make a script where when you purchase a gamepass in-game, you will recieve your reward without having to wait to rejoin the game. This is a tutorial on what to do and will only take about 5 minutes to learn.
As you all know, the first thing you usually do is start with variables. So we are gonna define our variables first, then start.
local MPS = game:GetService("MarketplaceService") local ID = 1234567890 -- This is where the gamepass ID is located.
Creating the function:
Now that we have our 2 variables, we can begin. The first line of code we now add is gonna be the function start.
MPS.PromptGamePassPurchaseFinished:Connect(function(purchasePassID, player, purchaseSuccess)
The parimeters we added in there were the basic stuff we would need. So basically purchasePassID is the ID in the receipt that was purchased. We are going to check if the ID is similar later. The next parimeter is player which is how we get it easily. This is how we can give/teleport/something with the player who bought it. The final parimeter is purchaseSuccess which is a name we must use that is in the Receipt. This name basically tells us if the purchase was successful or not.
With that, we must check if the purchase was true, and if the purchasePassID is the same ID as our variable. We can do that by simply saying:
if purchasePassID == ID and purchaseSuccess == true then
That will check, and if they are both true then the script will continue to run and we can do whatever we wish. This is when we put it the reward or thing they get from buying it.
In this end, if we combine everything together, it will look like:
local MPS = game:GetService("MarketplaceService") local ID = 7881322 MPS.PromptGamePassPurchaseFinished:Connect(function(player, purchasePassID, purchaseSuccess) if purchaseSuccess == true and purchasePassID == ID then --Code here if they buy it end end)
Now if you buy it in game, you will receive it right away without having to rejoin. Thanks to TheDevKing for teaching me about this.