Attempt to index nil with "UserId"

I’m trying to make a gamepass for a certain colored name tag in my cafe.But I am getting the error:Attempt to index nil with “UserId”
code:

local id = 13021767
local MPS = game:GetService("MarketplaceService")
script.Parent.MouseButton1Click:Connect(function(player)
	if MPS:UserOwnsGamePassAsync(player.UserId, id) then
		game.ReplicatedStorage.Events.RedNameTagEvent:FireServer()
	else
		MPS:PromptGamePassPurchase(player.UserId, id)
	end
end)

1 Like

MouseButton1Click does not return a player as an argument. If you are running this script from the Client just use LocalPlayer otherwise if you are in the server you should run the function in the client and fire a RemoteEvent to the server, that’ll pass the player argument.

3 Likes

On the remote event script i get the error [21:03:09.498 - Unable to cast value to Object]

local MPS = game:GetService("MarketplaceService")
local id = 13021767
game.ReplicatedStorage.Events.PurchaseRedNameTag.OnServerEvent:Connect(function(player)
	MPS:PromptGamePassPurchase(player.UserId, id)
end)

1 Like

The error is saying that your code is trying to cast a value, specifically a number, into an object, specifically a Player. It can’t do this.

The API for MarketplaceService::PromptGamePassPurchase says that the function takes in a Player instance as the first parameter. Your code is passing a UserId.

Try this:

local MPS = game:GetService("MarketplaceService")
local id = 13021767
game.ReplicatedStorage.Events.PurchaseRedNameTag.OnServerEvent:Connect(function(player)
	MPS:PromptGamePassPurchase(player, id)
end)

I did that right before you replied but it said third party sales have been disabled even though the game is owned by my group
gamepass: Red Name Tag - Roblox
group: Sunsét Café. - Roblox
game: Sunset Cafe - Roblox