Whats the best way to detect a gamepass purchase at a specific moment?

Hey. I am making a game, and in it, it prompts a gamepass, and the server needs to know if you buy it right then and there. I tried making my own script that does this, but it’s clearly bad and not the best for my case.

local function check(plr, id)
	mps.PromptGamePassPurchaseFinished:Connect(function(plrBought, boughtId, was)
		if plr == plrBought and id == boughtId and was then
			print('yes')
			rs.ConfrimPurchase:FireClient(plr)
		else
			print(plrBought.." is trying to buy "..id.." but the was was "..was.." and they are "..plr.." and id was "..id)
			print('no')
		end
		return
	end)
end

rs.RequestingPurchase.OnServerEvent:Connect(function(plr, id)
	task.spawn(check, plr, id)
end)

Its pretty bad ik, but i couldnt think of any other way. and im pretty sure this is exploitable in a way (?) if you can help me please do. Thanks

1 Like

The server should handle the request, but the client should handle prompting the purchase. As long as you’re confirming the purchase was finished from the server then it is not exploitable.

-- Client

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local passID = 0000000  -- Change this to your Pass ID
local function promptPurchase()
	local player = Players.LocalPlayer

	local success, response = pcall(MarketplaceService.UserOwnsGamePassAsync, MarketplaceService, player.UserId, passID)
	if not success then
		error("An error has happened: "..response)
	end

	if not response then
		-- Player does not own the Game Pass
		MarketplaceService:PromptGamePassPurchase(player, passID)
	end
end

-- Server

local MarketplaceService = game:GetService("MarketplaceService")

local passID = 0000000
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, gamePassId, wasPurchased)
	if gamePassId == passID and wasPurchased then
		-- Do something here
	end
end)

You can find more information here: Passes | Documentation - Roblox Creator Hub

I know how to prompt and receive, but I don’t know how I can detect if they purchase it right after prompt, because that’s how the game works. If a player is prompted a game pass, the server needs to know if they buy it right then.

So essentially you’re trying to find out if the purchase was successful when the prompt goes away? If so, the example i provided earlier will do just that. The Server example connects to PromptGamePassPurchaseFinished which will be fired whenever a Game Pass purchase is completed. In that function, the third arg is a boolean which is true or false based on whether or not the purchase was successful. If it was, you may do something like set data for the player.

When the game starts you check for the pass … sets a was owned flag.

I fully understand what you are trying to get to, but you are missunderstanding. I need to return whether it was purchased or not right then to the client. Would I supposedly fire a remote event?

Oh, i see. Sorry for the misunderstanding. Yes, the best way to notify the client would be to fire a RemoteEvent to the client, that way the server does all the checks and will tell the client whether or not it was successful.

1 Like

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