I wrote a code that implements a gamepass system. When the gamepass is bought, the player speed becomes 32, and the gamepass buy button destroys. Here’s the code
local MS = game:GetService("MarketplaceService")
local GamepassID = 228335926
local Player = game.Players.LocalPlayer
local BuyButton = script.Parent
local SuccessPurchase = BuyButton.Parent.SuccessPurchaseButton
BuyButton.MouseButton1Click:Connect(function()
MS:PromptGamePassPurchase(Player, GamepassID)
end)
MS.PromptGamePassPurchaseFinished:Connect(function()
Player.Character.Humanoid.WalkSpeed = 32
BuyButton:Destroy()
wait(3)
SuccessPurchase.Visible = true
wait(2.5)
SuccessPurchase:Destroy()
end)
Will it work as intended? Are there any edits that should be made?
I think it will work, but instead of using wait, do task.wait instead, as the normal wait will sometimes have a delay while task.wait will not. Hope this helps!
If you want to process purchases reliably, you have to use ProcessReceipt callback. PromptGamePassPurchaseFinished event fires when the prompt is closed, and it will fire event when the player pressed “Cancel”. It has 3 arguments: player, gamePassId, and wasPurchased. So if you want to determine whether a specific gamepass was purchased, you need to check gamePassId and wasPurchased arguments.
Also, all purchases made in studio will not charge your account, so you can safely test them.
You should add the other arguments that it gives you.
local MS = game:GetService("MarketplaceService")
local GamepassID = 228335926
local Player = game.Players.LocalPlayer
local BuyButton = script.Parent
local SuccessPurchase = BuyButton.Parent.SuccessPurchaseButton
BuyButton.MouseButton1Click:Connect(function()
MS:PromptGamePassPurchase(Player, GamepassID)
end)
MS.PromptGamePassPurchaseFinished:Connect(function(player,gamePassId,wasPurchased) -- You can do things with the three arguments.
Player.Character.Humanoid.WalkSpeed = 32
BuyButton:Destroy()
wait(3)
SuccessPurchase.Visible = true
wait(2.5)
SuccessPurchase:Destroy()
end)
Will it work as intended? like when it is bought, the player speed becomes 32, and when player clicks cancel, it doesn’t do anything. Will it work as intended?
Edit: I used " if wasPurchased == true then – do something " and it really worked! It did the code when I purchased the gamepass, and when I clicked cancel, it didn’t do anything. That’s exactly what I want! Thank you very much!