I was trying to make a devproduct. And when I tried to make it work when using a text button there is an error that says the player is nil. I cannot figure out why the player is nil or why this error is showing.
Error: MarketplaceService:PromptProductPurchase() player should be of type Player, but is of type nil
Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
-- References
local DevProductItems = ReplicatedStorage:FindFirstChild("DevProductItems")
-- Player
local Player = Players.LocalPlayer
-- Item
local DevProduct01 = 1174553817
local function onProducts(player)
MarketplaceService:PromptProductPurchase(Player, DevProduct01)
local UserBoughtProduct = MarketplaceService.PromptPurchaseFinished
if UserBoughtProduct then
local Backpack = player:WaitForChild("Backpack")
local StarterGear = player:WaitForChild("StarterGear")
local ProductItem = DevProductItems:FindFirstChild("ClassicSword")
ProductItem.Parent = StarterGear
ProductItem.Parent = Backpack
else
if not UserBoughtProduct then
print("DevProduct not owned")
end
end
end
script.Parent.MouseButton1Click:Connect(onProducts)
The reason must be because you are not getting the player correctly.
You should do things like print out what your player variable is, and try and see what you are setting it to and where.
Some of the potential reasons why your player variable might be wrong:
You are using Players.LocalPlayer in server code
You have arguments in the wrong place if your player variable is from a function argument
You misnamed a variable
If you are getting the player from a table you might be setting the value in the table after you try to get it
When you make a scripting support post you should include whatever code is relevant to the error, like, what your player variable is being set to or how you are getting it, and wherever you actually try to access the Player instance.
That makes spotting the exact issue possible. People won’t be able to know what problem you’re having if they can’t see what is causing it, and sometimes problems like this can be confusing or misleading so its good to be able to see the code.
Your problem is because the MouseButton1Click event doesn’t pass a player variable to its callback (and also you’re using two different player variables with different names):
Also, PromptPurchaseFinished is an Event not a boolean, you can connect a callback to it but you can’t use it like you are trying to. It fires whenever a purchase finishes. You should look at the documentation: MarketplaceService.PromptPurchaseFinished
You also can’t use Players.LocalPlayer on the server if you are trying to, because it means whoever is running the code. If you are on the server, the server is running the code, and your code isn’t on someone’s computer running.