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).
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)
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.