Prompting a User to Buy a Gamepass on Touch

How would I go about making it so that when a user hits an object, it will check if they own a gamepass, and then if they do NOT own it, it prompts them to buy it?

We have a specific area for people with a VIP gamepass, so how can I make it ask them to buy it?

Any help is welcome, as I am basically helpless at scripting.

1 Like

Userownsgamepassasync (remember to use a pcall)

If not , then prompt him

You can use UserOwnsGamePassAsync and PromptGamePassPurchase with pcall, as UMirinBrah said above

1 Like
local players = game:GetService("Players")
local marketplace = game:GetService("MarketplaceService")
local userOwnsGamePass = marketplace.UserOwnsGamePassAsync
local promptGamePassPurchase = marketplace.PromptGamePassPurchase

local part = script.Parent

local gamePassId = 0 --Change to gamepass's ID.

local function onTouched(hit)
	local hitModel = hit:FindFirstAncestorOfClass("Model")
	if not hitModel then return end
	local hitPlayer = players:GetPlayerFromCharacter(hitModel)
	if not hitPlayer then return end
	local hitHuman = hitModel:FindFirstChildOfClass("Humanoid")
	if not hitHuman then return end
	if hitHuman.Health <= 0 then return end
	local success, result = pcall(userOwnsGamePass, marketplace, hitPlayer.UserId, gamePassId)
	if success then
		if not result then --User doesn't own the gamepass.
			local success2, result2 = pcall(promptGamePassPurchase, hitPlayer, gamePassId)
			if success2 then
				if result2 then
					return
				end
			else
				warn(result)
			end
		else --User already owns the gamepass.
			--Do code.
		end
	else
		warn(result)
	end
end

local function onPromptGamePassPurchaseFinished(player, _gamePassId, wasPurchased)
	if wasPurchased then
		if _gamePassId == gamePassId then
			--Imburse the user the goods they purchased.
		end
	end
end

part.Touched:Connect(onTouched)
marketplace.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)

Server script inside a BasePart instance.