Gamepass prompt

Hi. I wanna make a gamepass prompt but it keeps saying attempted to nil with UserId
What do I do?

local Items = {
	
	x2Dice = {
		Name = "x2 Dice",
		Cost = 500
	},
	Custom = {
		Name = "the Custom Skin",
		Cost = 200
	}
}
workspace.Server.Purchase.OnServerInvoke = function(p, item)
	
	local i = Items[item]
	local g = 11592650
	local MarketPlaceService = game:GetService("MarketplaceService")
	local Player = game.Players.LocalPlayer
	local Haspass = MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, g)
	if not i then
		warn("Item "..item.." doesn't exist")
		return {
			{TX = {"Error - you have not been charged."}}
		}
	end
	if item == "Messor" and p.Data.Hats:FindFirstChild("Messor") then
		return {
			{TX = {"You already have the Messor."}}
		}
	elseif item == "Debug" then
		return {
			{TX = {"This item has been discontinued and may be brought back at a later date."}}
		}
	end
	if Haspass then
		return {
			{TX = {"You've already purchased ","TXEFX:TextColor3:#FF0000",i.Name.."."}}
		}
	else
		MarketPlaceService:PromptGamePassPurchase(Player, g)
		end
	
		return {
			{TX = {"You have purchased ","TXEFX:TextColor3:#FF0000",i.Name.."!"}}
	}
end
local g = 11592650
local MarketPlaceService = game:GetService("MarketplaceService")
local Player = game.Players.LocalPlayer
local Haspass = MarketPlaceService:UserOwnsGamePassAsync(Player.UserID, g)
if Haspass then
	print("Has the pass.")
end

thats all of the script i think

Please be aware that your code is pretty messy.
Also, please highlight the line that causes the error in the future.

I assume that this is what causes the error, correct?

You instanced
local Player = game.Players.LocalPlayer
But this is something you can’t do in a server script. Your OnServerInvoke function it’s first parameter gives you the player that invoked the server, which you named “p”

So I believe your error would be fixed if you changed the quoted line to:
MarketPlaceService:PromptGamePassPurchase(p, g)

1 Like