How to succesfully listen for when a player prompted finishes his decision of buying the gamepass

	if PaymentType == "Robux" then
		local GamePassId = Item.Info.PaymentInfo.GamePassId.Value
		
		MarketplaceService:PromptGamePassPurchase(Player, GamePassId)
		local playerwhoPurchased, Id, Purchased = MarketplaceService.PromptGamePassPurchaseFinished:Wait()
		
		if playerwhoPurchased == Player then
			if Id == GamePassId then
				if Purchased then
					return true
				else
					return false
				end
			end
		end
		
	end

Heres what I got so far the problem with this is that someone else could buy the gamepass it would still fire and return false, is it possible to detect only if a certain player buys a certain gamepass

Just listen for the event on the server it will fire with all the arguments, and then hook it up to a function that processes the game pass purchase.

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(playerwhoPurchased, Id, Purchased)
      --then you give the players what they should get if they purchased it
end)

I cant just use :Connect() because I have to yield the script till a player purchases something.

Why would you yield it?

30chars

No you would not have to, the prompt gamepass purchase function yields already until the user selects an option.

Because this is a remote function the client side has to wait till the server checks if the gamepass was ourchased, if the item was purchased on the client ui it says Item purchased

you should prompt gamepass purchases on the client not the server

Ok let me test it out if it works Ill tell you

It will i have worked with gamepasses before.


https://gyazo.com/22c0df9cb67bca14994340863ca8de72

Nope this does not yield the script

	if PaymentType == "Robux" then
		local GamePassId = Item.Info.PaymentInfo.GamePassId.Value
		
		MarketplaceService:PromptGamePassPurchase(Player, GamePassId)
		MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, GamePassId,Purchased )
			print("purchased")
			
		end)
		
		print "s"
		
	end

Im pretty sure :Connect() doesnt yield a script thats why I told you I needed to use :Wait()

This happened when exactly after I was prompted s was printed

no your still doing it wrong you prompt on the client and listen on the server

You can fire back the exact event when the purchase was finished

Client

RequestPurchase:FireServer(GamePassId);

RequestPurchase.OnClientEvent:Connect(function(completed)
    if completed then
        textLabel.Text = "Purchased"
    end;
end)

Server

RequestPurchase.OnServerEvent:Connect(function(Plr, GamePassId)
    MarketplaceService:PromptGamePassPurchase(Player, GamePassId);
end);

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Plr, Id, Purchased)
    if Purchased then
        RequestPurchase:FireClient(User, true);
else
        RequestPurchase:FireClient(User, false);
    end
end)
1 Like

There is no reason for you to be prompting game pass purchases on the server

So basically I prompt the gamepass in the client wait till its finished and then fire a remote event to the server when the thing is purchased then the server gives the player the item?

no in a local script you do this:

MarketplaceService:PromptGamePassPurchase(Player, GamePassId);

and in a server script you listen

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Plr, Id, Purchased)
--the id argument is the gamepass id
-- the purchased bool tells you if it was bought or not
--player argument is player that was prompted
end)
3 Likes

I cant really listen to this, this is part of a bigger script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local AntiExploitModule = require(game.ServerScriptService.ServerModules.AntiExploitModule)

local Shopitems = game:GetService(ā€œReplicatedStorageā€).GameTools

local HttpService = game:GetService(ā€œHttpServiceā€)
local MarketplaceService = game:GetService(ā€œMarketplaceServiceā€)

local WebHook = ā€œhttps://discord.com/api/webhooks/719313410214592593/FMYhiD4QWjdqFFlRrTYPz5SqLHA7ZMebXdKp6UFsw1CKdCfsFUeZ2zZ8_t0nFN1jTHxVā€

ReplicatedStorage.ShopEvents.CheckPrice.OnServerInvoke = function(Player, Item, Folder)
local WasExploiting = AntiExploitModule.CheckLeaderstats(Player)

if WasExploiting == false then
	local Item = Shopitems[Folder][Item]
	
	local PaymentType = Item.Info.PaymentInfo.PaymentType.Value
	
	
	local Price  = Item.Info.PaymentInfo.Price.Value

	local DoesPlayerAlreadyHaveitem

	if Player:WaitForChild("SwordsOwned"):FindFirstChild(Item) then
		DoesPlayerAlreadyHaveitem = true
	else
		DoesPlayerAlreadyHaveitem = false
	end

	if DoesPlayerAlreadyHaveitem then
		return "AlreadyOwned"
	else
		
		if PaymentType == "Points" then
			if Player.leaderstats.Points.Value >= Price then
	
				Player.leaderstats.Points.Value  = Player.leaderstats.Points.Value  - Price
		
				local ItemOwnedTag = Instance.new("StringValue")
				ItemOwnedTag.Value = Item
				ItemOwnedTag.Name = Item
				ItemOwnedTag.Parent = Player.SwordsOwned
		
	
				return true
			else
				return false
			end
		end
	end
	
	if PaymentType == "Robux" then
		local GamePassId = Item.Info.PaymentInfo.GamePassId.Value
		
		MarketplaceService:PromptGamePassPurchase(Player, GamePassId)
		MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, GamePassId,Purchased )
			print("purchased")
			
		end)
		
		print "s"
		
	end
	
	
elseif WasExploiting == true then
	local ThingtoSend = {
			["content"] =   Player.Name .. " tried to change his leaderstats value! CAUGH (doesnt really matter it doesnt replicate to the server)"
		
		}
		
		ThingtoSend = HttpService:JSONEncode(ThingtoSend)
		HttpService:PostAsync(WebHook, ThingtoSend)
	
	return false
end

end

You should prompt the game pass on the client. Have a listener on the server waiting for a purchase. If a purchase comes send a remote event back to the player telling them to update their UI. You also give the player their reward on the server.

yah ok Ill try that i guess

30 chars