A little help on a script. (1 line)

Hello there! I have been looking at youtube tutorials and just cant find out what the error is with my script. Its supposed to when you click show you that you can buy a gamepass but i tried using some prints. Everything printed exept for print 5 here is the script:

local plr = game.Players.LocalPlayer
print(“print1”)
local button = script.Parent
print(“print2”)
local MarketplaceService = game:GetService(“MarketplaceService”)
print(“print3”)
script.Parent.MouseButton1Click:Connect(function()
print(“print4”)
MarketplaceService:PromptGamePassPurchase(plr, 0000000) --id instead of 0000000
print(“print5”)
end)

2 Likes

Highly recommend watching devking: https://youtu.be/3hMmhmC18RA

AFAIK you can’t prompt gamepass purchases on the client (LocalScript). You have to call a RemoteEvent to tell the server that the client calling it wants to buy an item.

Instead of just providing where the script failed could you give any errors that were logged in the Output. 95% of the time these clarify the issue rather quickly.

in output it just says
MarketplaceService:PromptGamePassPurchase() player should be of type Player, but is of type nil

Your script is most likely trying to fire the PromptGamePassPurchase(Player) function before the player is loaded.

Kind of a sloppy fix:

local MarketplaceService = game:GetService(“MarketplaceService”)
local Players = game:GetService("Players")

game:GetService("RunService").Heartbeat:Wait() -- not really necessary / practical

local plr = Players.LocalPlayer

local button = script.Parent

script.Parent.MouseButton1Click:Connect(function()
    MarketplaceService:PromptGamePassPurchase(plr, id)
end)

wouldnt i instead of this use
Players.PlayerAdded:Connect(Spawned)

No, PlayerAdded won’t fire for the LocalPlayer.

When testing in studio and i already own the gamepass and its in team create is it supposed to say “You already own this item. Your account has not been charged.”?

Try this method instead:

Create a RemoteEvent inside of ReplicatedStorage called “PromptPurchase”. Then, create a ServerScript inside of ServerScriptService with the following code:

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

local promt = ReplicatedStorage:WaitForChild("PromtPurchase")
local passId = -- your GamePassId

promt.OnServerEvent:Connect(function(player)
    MarketplaceService:PromptGamePassPurchase(player, passId)
end)

Then, in a LocalScript inside of your button, write the following code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local prompt = ReplicatedStorage:WaitForChild("PromptPurchase")

script.Parent.MouseButton1Click:Connect(function()
    prompt:FireServer()
end)

I scripted this quickly without any testing, so please let me know if you run into any errors.

1 Like

is it supposed to say that in team create?

If you already own the gamepass, it will let you know. But in playtesting you should get a “Test Purchase” prompt.

im sorry for waisting everybodys time but it was because i was using a regular Script and not a LocalScript i am so sorry for my stupidity!

1 Like

You need to use a ServerScript (regular script) for the prompt, but use a LocalScript for the button. Did you try my method?

No worries! I got it working because i realized it was a Script instead of a LocalScript!