Marketplace Service Sometimes doesn't work

Hi, I made a store in the game where you can use a proximity prompt to trigger a purchase. The code is suppose to go through all the items and check if you own it using the Marketplace Service API. For some reason it doesn’t always return a result.

Here is the code

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

local Remotes = ReplicatedStorage:WaitForChild("Remotes")

local Equip = Remotes:WaitForChild("Equip")

local WeaponsFolder = workspace:WaitForChild("DisplayWeapons")

local function HasGamepass(GamepassId)
	local Success,Results = pcall(function()
		return MarketplaceService:UserOwnsGamePassAsync(Players.LocalPlayer.UserId, GamepassId)
	end)

	if Success and Results then
		return true
	end
end

for Index, Weapon in ipairs(WeaponsFolder:GetChildren()) do	
	local Prompt: ProximityPrompt = Weapon:FindFirstChild("ProximityPrompt")
	local Id: IntValue = Weapon:FindFirstChild("GamepassId").Value

	if HasGamepass(Id) then
		Prompt.ActionText = "Equip"
	else
		Prompt.ActionText = "Buy"
	end

	Prompt.Triggered:Connect(function() PromptGamepassPurchase(Id) end)
end

I’m pretty sure this is because I’m calling the API too much. Not quite sure, need some help with this!

1 Like

You should use warn if a pcall fails. There’s a good starting point for eyeing this down.

local Success,Results = pcall(function()
  return MarketplaceService:UserOwnsGamePassAsync(Players.LocalPlayer.UserId, GamepassId)
end)

if not Success then
  warn(`Failed to check gamepass {GamepassId}: {Results}`)
  return false
end
2 Likes

The problem is that the line of code below does not return anything

return MarketplaceService:UserOwnsGamePassAsync(Players.LocalPlayer.UserId, GamepassId)```
1 Like

Are you sure you’re getting a pcall response of true, nothing?

Thats, not how that UserOwnsGamePassAsync should work, it should always return. If it doesn’t, its almost definitely an error, which is why I said you should output the result of the pcall for debugging sake.

1 Like

I tried printing the result of UserOwnsGamePassAsync, it only sometimes work. I have another script using it as well, and it works fine. So I don’t know if I am calling it too much

1 Like

So I think I found the issue, the for loop is not running everytime, probably because the game hasn’t loaded

1 Like

Yeah this was the problem, fixed now!

1 Like

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