MarketplaceService

My script is doing something I don’t know why its doing.

In the server, this is my code:

local remote = game:GetService(“ReplicatedStorage”):WaitForChild(“purchaseAnimation”)
local mpS = game:GetService(“MarketplaceService”)
remote.OnServerInvoke = function(plr, id)
mpS:PromptGamePassPurchase(plr,id)
local connection
connection = mpS.PromptGamePassPurchaseFinished:Connect(function(player,gamePassId,purchased)
if plr == player and gamePassId == id then
connection:Disconnect()
return purchased
end
end)
end

I have it so when I press a button, it fires this remote function and it prints the result. It is supposed to fire after the choice to buy the gamepass has been made. then it returns the choice. When I run the code, it just returns blank. No error!

Are you sure that the checks for plr == player and gamePassId == id are both actually true or does it not get past that check?

1 Like

Well from what the Dev Forum says, it will stop the client code from running until it has returned. Because it has printed, I know it has passed the check. I put that there in case any other gamepass prompt gets made.

" If the result is not needed, we recommend that you use a RemoteEvent instead, since its call is asynchronous and doesn’t need to wait for a response to continue execution. See Remote Functions and Events for more info" found on RemoteFunction | Roblox Creator Documentation

The issue is that you are returning the purchased state to the connection, and not to the main function. You should put the gamepass purchase handler in a different script, and use remote events and FireClient

I don’t get what you mean. I am trying to see if someone bought the gamepass. When the gamepass finished thing gets fired, I look to see if its the right gamepass, then I return if it was purchased.

local remote = game:GetService(“ReplicatedStorage”):WaitForChild(“purchaseAnimation”)
local mpS = game:GetService(“MarketplaceService”)
remote.OnServerInvoke = function(plr, id)
mpS:PromptGamePassPurchase(plr,id)
local purchasedState
local connection
connection = mpS.PromptGamePassPurchaseFinished:Connect(function(player,gamePassId,purchased)
if plr == player and gamePassId == id then
connection:Disconnect()
purchasedState = purchased
end
end)
repeat wait() until purchasedState
return purchasedState
end

This is an example of a fix. The issue is that you are using return in the :Connect.
However, this is not very efficient and it will be better to use remote events to tell the client that the purchase went through

I get what you mean. Ill try that