Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

The full Error:

MarketplaceService:PromptProductPurchase() player should be of type Player, but is of type nil

The code:

local Players_ = game:GetService("Players")
Players_.PlayerAdded:Connect(function(plr)
	TheLocalPlayer = plr
end)
local Market = game:GetService("MarketplaceService")
script.Parent.Activated:Connect(function()
	Market:PromptProductPurchase(TheLocalPlayer, 1130818996)
end)

This is a LocalScript and it's in: StarterGui>ScreenGui>Frame>TextButton>LocalScript

Note: I probably know how to do a workaround for this, but I do want to know exactly why this happens so I can educate myself on that. Thanks! :D

1 comment

0

I think you have to use normal Scripts to prompt purchase instead of local scripts, also normal scripts won't work in startergui so put it in serverscriptservice

2 answers

the error means the typeof(player) is nil and not a player instance, so fix as shown and it should work:

local Players = game:GetService("Players")
local TheLocalPlayer = nil
Players.PlayerAdded:Connect(function(plr)
    TheLocalPlayer = plr
end)
local Market = game:GetService("MarketplaceService")
script.Parent.Activated:Connect(function() -- not sure what you want activated for a prompt to show up but here we are
	if TheLocalPlayer ~= nil then
   	 Market:PromptProductPurchase(TheLocalPlayer, 1130818996)
	else print("localplayer was nil") end
end)

didnt test this and never used prompt purchase so it may work on the client but i think you get my premise

1 comment

0

I know how to fix it, I just wanted to know why it happens.

When you declare a variable without using local, Roblox makes it local for you. This is both a benefit and a pain to deal with if you plan on actually declaring global variables (but _G exists for this reason).

In terms of how to resolve your issue, you are going to need to declare a blank variable on the base scope (the variable doesn't have to be assigned to anything to be declared) and then assign its value when the time comes.

This discussion is preserved as it was. New answers, comments, and votes are closed.