Problem with PromptGamePassPurchase

Hello all,

I am creating a script that if you do not have the gamepass then it will prompt you to buy it. I am having a issue with the script that comes up with an error saying " Unable to cast value to Object". I feel like I have had this problem before but cannot remember on what I did to fix it.

The error is linked to the Prompt Purchase part so you know. Bellow is my script (in a local script):

local player = game.Players.LocalPlayer

local button = script.Parent

local marketplace = game:GetService("MarketplaceService")

local player = game.Players.LocalPlayer

local playerid = player.UserId

button.MouseButton1Click:Connect(function()

print("TraineeButtonClicked")

print(playerid)

if marketplace:UserOwnsGamePassAsync(playerid, 13719925) then

print("User has the gamepass!")

else

print("User does not have the gamepass!")

marketplace:PromptGamePassPurchase(playerid, 13719925)

end

end)

Hello!
The error is happening because you are trying to place a UserId as a argument, where should be given a Player object!
So the fix will be you replacing this line with marketplace:PromptGamePassPurchase(player, 13719925).

Have a great rest of your day!

2 Likes

In addition to what is already said, I believe that when you check if they own the game pass, the function can yield, so wrap it in a pcall.

Could you explain that more? I am quite new to scripting sorry.

A pcall ends the function if an error is encountered, instead of throwing an error and yielding the thread. Essentially, it is a backup system. So instead do:
pcall(function()
— Put code here
end)

But surely you would want the error so you can fix it??

The error that would be given would have to do with a problem in Roblox’s systems. Same thing with datastores; they fail. Also, it would yield the thread, stopping any further lines of code from running.