I have a shop where you can buy a gamepass. When I buy it while testing, it doesn’t follow through. Nothing is printed, and the success sound won’t play. There are no errors. Does anyone know how to fix? I see there are other posts about this, but I don’t know how to implement a proper fix in this case.
code:
if v.isGamepass.Value == true then
if mkps:UserOwnsGamePassAsync(plr.UserId, v.gamepassid.Value) then
display.Price.Text = "Owned"
display.Buy.Visible = false
else
display.Buy.Visible = true
display.Price.TextColor3 = Color3.fromRGB(0, 255, 0)
display.Buy.MouseButton1Click:Connect(function()
mkps:PromptGamePassPurchase(plr, v.gamepassid.Value)
mkps.PromptGamePassPurchaseFinished:Connect(function(plr, purchasedPassID, purchaseSuccess)
if purchasedPassID == v.gamepassid.Value and purchaseSuccess == true then
print("player bought it")
main.Success:Play()
display.Price.Text = "Owned"
display.Buy.Visible = false
end
end)
end)
end
else
Correct me if I’m wrong, but I’m pretty sure game pass PromptGamePassPurchaseFinished will never fire in studio. You’ll need to publish your place and test in-game to test if the logic actually works.
Well it kinda looks like a LocalScript with all these display.Buy.Visible = false and etc. Maybe there’s an issue with gamepass ID or the isGamepass value, if it doesn’t print out anything?
Do these functions that change button text and visibility work, @1962_Mustang ? If not, then there’s a problem with values, if yes, then I dunno, try to place the script somewhere else or smth
If you try to trigger the gamepass with the wrong ID, the MPS will issue an error.
In general, it is difficult to say anything without details and full code.
Mostly everything works fine. It prompts it, the gui text stuff works; etc. The only issue is that the MPS Finished event isn’t working–the Succes sound doesnt play, the text doesnt change to “owned,” so I don’t know what to do. I even tested it in the actual game and it doesnt work, yet there are no errors.
I appreciate the reply, but there aren’t any errors. Here is the full code of the shop (work in progress):
local display = script.Parent
local main = display.Parent
local buttonsfolder = main.guns.Buttons
local plr = game.Players.LocalPlayer
local mkps = game:GetService("MarketplaceService")
local rs = game:GetService("ReplicatedStorage")
for i, v in pairs(buttonsfolder:GetChildren()) do
if v:IsA("TextButton") then
v.MouseButton1Click:Connect(function()
main.Sound:Play()
local gunName = v.gunName.Value -- getting selected item info
local gunPrice = v.gunPrice.Value
local gunDesc = v.gunDesc.Value
display.gName.Text = gunName -- putting it on the display
display.Price.Text = gunPrice
display.Desc.Text = gunDesc
if v.isGamepass.Value == true then
if mkps:UserOwnsGamePassAsync(plr.UserId, v.gamepassid.Value) then
display.Price.Text = "Owned"
display.Buy.Visible = false
else
display.Buy.Visible = true
display.Price.TextColor3 = Color3.fromRGB(0, 255, 0)
display.Buy.MouseButton1Click:Connect(function()
mkps:PromptGamePassPurchase(plr, v.gamepassid.Value) -- it prompts correctly
mkps.PromptGamePassPurchaseFinished:Connect(function(plr, purchasedPassID, purchaseSuccess) -- this is where it stops working
if purchasedPassID == v.gamepassid.Value and purchaseSuccess == true then
print("player bought it")
main.Success:Play()
display.Price.Text = "Owned"
display.Buy.Visible = false
end
end)
end)
end
else
display.Price.TextColor3 = Color3.fromRGB(255, 183, 0) -- if not gamepass item, change the color to Tix color
display.Buy.MouseButton1Click:Connect(function()
if plr.leaderstats.Tix.Value >= gunPrice then
rs.buyItem:FireServer(plr, gunName, gunPrice)
end
end)
end
end)
end
end
PromptGamePassPurchaseFinished event cannot be caught by the client, this is done for security purposes.
Use remote events to transfer information about the pass and the player to the server script and use that script to show the payment window and process it (payment) there
DOCS: RemoteEvent | Documentation - Roblox Creator Hub
The above code connects the Buy button being clicked to a RemoteEvent that is being fired to the server. This connection is made in a MouseButton1Click connection. This means if v (in the loop) is clicked multiple times, the display.Buy button connection will be made multiple times. This means if the player clicks v let’s say 5 times, when they click the display.Buy button, it will Fire the RemoteEvent 5 times because for every time v is clicked, another connection or “listener” is being created for display.Buy.MouseButton1Click.
Yep, it can only be on the server. You can use a RemoteEvent to notify the client of a successful purchase.
I added a solution with a remote event a little later
My method assumes that the button click event processing is in the client and the payment processing is on the server. Your option (sending a request to the client that the purchase was successful) is also great, because it will allow to interact with the client in case of a successful purchase(without any problems)