Unable to cast value to object

local gamepass = game:GetService("GamePassService")
local market = game:GetService("MarketplaceService")

function leftClick()
	local player = game.Players.PlayerAdded:Connect(leftClick)
	market:PromptGamePassPurchase(player, 1057151)
end

script.Parent.MouseButton1Click:Connect(leftClick)

Console Error
Unable to cast value to Object

:Connect returns an rbxscriptconnection, which you can use to disconnect the connection. You can use LocalPlayer in local scripts instead.

Also by the way GamePassService is deprecated and shouldn’t be used because marketplace service has everything you need.

2 Likes

You are incorrectly getting the player because if this script is being ran on the client the PlayerAdded event wont fire when you join the game because you have already joined the game before it has a chance to be fired. Instead you should use the built in method of getting the player on the client: game:GetService("Players").LocalPlayer.

Here is your code fixed:

local gamepass = game:GetService("GamePassService")
local market = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

function leftClick()
	local player = Players.LocalPlayer
	market:PromptGamePassPurchase(player, 1057151)
end

script.Parent.MouseButton1Click:Connect(leftClick)
1 Like