Prompt gamepass purchase is not working

Im making a script that you need a gamepass to teleport to another place but when it comes to PromptGamePassPurchase it says this:
image

This is the script:

local TravelGui = script.Parent
local teleportService = game:GetService("TeleportService")
local player = game.Players.LocalPlayer
local MarketplaceService = game:GetService("MarketplaceService")

local NorthernValley = TravelGui.NorthernValley

-- Place ID --

local PlaceID1 = 13325072959

-- GamepassID --

local GamepassID = 214187352

--------------

NorthernValley.MouseButton1Up:Connect(function()
	if MarketplaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then
		teleportService:Teleport(PlaceID1, player)
	else
		MarketplaceService:PromptGamePassPurchase(player.UserId, GamepassID)
	end
	
	if MarketplaceService.PromptGamePassPurchaseFinished then
		teleportService:Teleport(PlaceID1, player)
	end
end)

I have been trying to fix it so many times and its still not working. so can someone help me with it?

1 Like

PromptGamePassPurchase takes the Player Instance and not the Player’s UserId as a value.

1 Like

Thanks for the help but rn when i dont have gamepass to teleport to this place it teleports me before the prompt purchase pops up. So is there also solution for that?

This right here is the problem. MarketplaceService.PromptGamePassPurchaseFinished is not a boolean, it is a signal. It runs the code inside the if operator because it’s value isn’t nil. You need to connect it to a function like this:

MarketplaceService.PromptGamePassPurchaseFinished:Once(function(Player, ID, WasPurchased)
	if ID == GamepassID and WasPurchased then
		teleportService:Teleport(PlaceID1, player)
	end
end)

However, incase you got multiple buttons to teleport to multiple places, I recommend you use a single function, instead of writing the same code over and over. Like this:

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

local Player = game.Players.LocalPlayer
local TravelGui = script.Parent

local TravelButtons = {
	[TravelGui.NorthernValley] = {PlaceID = 13325072959, GamepassID = 214187352},
	[TravelGui.OtherValley] = {PlaceID = 99999999, GamepassID = 123456},
	[TravelGui.SecondValley] = {PlaceID = 1234567, GamepassID = 876543},
}

function Travel(Button)
	local Data = TravelButtons[Button]
	local PlaceID = Data.PlaceID
	local GamepassID = Data.GamepassID
	
	if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GamepassID) then
		TeleportService:Teleport(PlaceID, Player)
	else
		MarketplaceService:PromptGamePassPurchase(Player, GamepassID)
	end
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(_, ID, WasPurchased)
	if not WasPurchased then return end
	for Button,Data in pairs(TravelButtons) do
		if Data.GamepassID ~= ID then continue end
		TeleportService:Teleport(Data.PlaceID, Player)
	end
end)

for Button,Data in pairs(TravelButtons) do
	Button.MouseButton1Click:Connect(function()
		Travel(Button)
	end)
end

^Only downside with this is if you can buy the gamepass ingame, but you don’t want to travel right away(maybe if you have a gamepass store and you can buy the travel gamepass there, this might be the case). To fix that, you would have to check inside of the PromptGamePassPurchaseFinished function if the TravelGui is currently open, or something like that.

1 Like

Thank you so much, you helped me a lot :slight_smile:

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