Can you guys review my code and tell me if it's going to work with no errors or bugs?

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?

6 Likes

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!

3 Likes

Short answer: No.

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.

5 Likes

Alright, I’m gonna test them.

edit: you’re right. It fires even when I clicked cancel. Is there a simple way to make it work as intended? Idk how to use ProcessReceipt

Is there an event that fires when the gamepass is purchased?

2 Likes

Some guy said that it won’t work.

Im gonna test it and see

edit: man it doesn’t work as intended.

2 Likes

What do you mean it doesn’t work? task.wait will just make it more efficient.

2 Likes

I mean, the code itself doesn’t work.

You said it would work.

It fired the event, even when I clicked " Cancel ".

Is there a way to make it fire when the player purchases the gamepass?

1 Like

Try using the wasPurchased boolean at the arguments and check if it’s true.

At PromptGamePassPurchaseFinished

(when you purchase it, wasPurchased should return true, if player clicked cancel, it should return false)

2 Likes

should I only add the wasPurchased boolean and not do any other edits?

If no, can you please add the wasPurchased boolean, make the necessary edits send my code again?

I would be very grateful:)

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)

Got something wrong, sorry.

1 Like

And should it work as intended? Are you sure?

I used the PromptGamePassPurchase function with a gamepass id, and when i clicked buy, it returned true (i printed it).

When the player clicks cancel, it returns false.

Try printing the wasPurchased boolean when the purchased is finished.

So, it’s going to work. Alright thank you so much!

No problem! still try printing the boolean though.

1 Like

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?

Most likely, you should check if wasPurchased is true and then do the code u want to do, if not, u do anything there.

Your code shouldn’t be wrong, it should normally work.

I’ll test it and see, but later.

Thanks!

1 Like

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!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.