"player should be of type Player, but is of type nil"

So I wanted to prompt the player with a purchase right when they join the game but I got this weird error message that is in the title so how do I fix this problem?

Script
local MarketPlaceService = game:GetService("MarketplaceService")

local gamepassID = --whatever my gamepass ID is

local function promptPurchase()
	
	local player = game.Players.LocalPlayer
	local hasPass = false
	
	local success, errormessage = pcall(function()
		hasPass = MarketPlaceService:UserOwnsGamePassAsync(player.UserId, gamepassID)
	end)
		
	if hasPass then
		print("player has the pass, can't prompt")
	else
		MarketPlaceService:PromptGamePassPurchase(player, gamepassID)
	end
end

wait(3)
promptPurchase()

hyhewheuw

4 Likes

No, it’s name is MarketplaceService. Don’t make posts about what you don’t know.

1 Like

No it won’t work because that’s literally the service’s name.

1 Like

Where the error says, try:
PromptGamePassPurchase(player.UserId, gamepassID)

Maybe?

1 Like

No, it needs the player Instance not it’s userid. And please, stop making random posts.

1 Like

Alright, just trying to be helpful! :+1:

1 Like

If I do that it will give me an error saying:

Attempt to index nil with ‘UserId’

I don’t even know why it’s nil…

1 Like

I have included the script in the description but it’s just hidden click on the “Script” text to open it…

1 Like

Are you sure you are using a Local Script? player will be nil if it is in a normal Script.

5 Likes

Oh I actually put it in a Script instead of a Local Script thank you you fixed the problem when I tried to put it in a Local Script it worked!

3 Likes