2
votes

I am making a game. I am trying to make a script that will prompt the player to buy a gamepass when they click a button on a SurfaceGUI. However, the function isn't executing, and I don't know why.

If it helps, it is in a LocalScript.

script.Parent.MouseButton1Click:Connect(function()
    local mps = game:GetService("MarketplaceService")
    local player = game.Players.LocalPlayer
    print("vars")
    mps:PromptGamePassPurchase(player, 10772382)
    print("prompt")
end)

2
Is this LocalScript located somewhere in the Workspace? I'm assuming that since it's a child of a SurfaceGui, that it's located somewhere in the Workspace. - Kylaaa

2 Answers

2
votes

The issue is that this is a LocalScript, and LocalScripts only execute from a few locations. According to the docs :

A LocalScript will only run Lua code if it is a descendant of one of the following objects:

  • A Player’s Backpack, such as a child of a Tool
  • A Player’s character model
  • A Player’s PlayerGui
  • A Player’s PlayerScripts.
  • The ReplicatedFirst service

So to fix your issue, convert this LocalScript to a Script, and listen for a player to join to get access to the player object.

local mps = game:GetService("MarketplaceService")
local ps = game:GetService("Players")

ps.PlayerAdded:Connect(function(player)
    script.Parent.MouseButton1Click:Connect(function()
        print("vars")
        mps:PromptGamePassPurchase(player, 10772382)
        print("prompt")
    end)
end)
0
votes

I do not know why your current script is not working however, here is a script I use that works well(just change the ID value to your id local id = 6604880

script.Parent.MouseButton1Click:Connect(function()
    game:GetService("MarketplaceService"):PromptGamePassPurchase(game.Players.LocalPlayer,id)
end)