Gamepass BuyPromt not working correctly

Hello, I have this on a script that detects when you click a button, it does print "you have it if I own it but if I dont and I use the buy promt it dosent register the purchase for the first 3 times so i end up geting like 3 prompts in a row and until the 3rd “buy” click it prints print “you have it” how can I fix this ? Thanks for the help! (and in studio it dosent work I keep geting the prompts infinitely when trying to buy it)

if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 2043661) then
	print("You have it")
else
game:GetService("MarketplaceService"):PromptGamePassPurchase(player, 2043661)
print("you dont have gamepass")
end


1 Like

The result of UserOwnsGamePassAsync caches for 10 seconds after a call if the result is false, so every subsequent call within these 10 seconds returns the cached value. In your case, you have an if statement that runs UserOwnsGamePassAsync and if it returns false, it prompts the user to buy the pass. That false result is held for 10 seconds. This is why, only after a few seconds/attempts, it finally hands you the result you expect.

Your code is working perfectly fine and as intended. There is no bug, issue to fix nor a way to clear the cache manually. Just be wary of this cache and work around it if you can.

As colbert has said, this is due to cached values. You can work around this by using PromptGamePassPurchaseFinished to listen for purchases of the said game pass and add them to your own cache table.

For example, you could use this to cache purchases:

local ownerCache = {}
 
game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(player, passId, purchased)
	if purchased and passId == 2043661 then -- Check they actually purchased it
		ownerCache[player.UserId] = true
	end
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
	ownerCache[player.UserId] = nil -- Clear up small memory leak
end)

and in the same script have your button click logic as:

if ownerCache[player.UserId] or game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 2043661) then
	ownerCache[player.UserId] = true -- Prevent unnecessary API calls
	print("You have it")
else
	game:GetService("MarketplaceService"):PromptGamePassPurchase(player, 2043661)
	print("you dont have gamepass")
end

Doing this will avoid the cached values that have been causing a problem for you.

1 Like

Thanks, I will try that and see if it works