Hey! I am starting my Lua scripting journey, and wanted to make a Developer Product Purchase script
Well, this is a localscript in a ScreenGUI, with a button inside of the ScreenGUI named ‘Die’
Here was the script:
local MPS = game:GetService("MarketplaceService")
local Storage = game:GetService("ReplicatedStorage")
local Die = script.Parent.TextButton
local player = game.Players.LocalPlayer
local Event = Storage:WaitForChild("RemoteEvent")
local function Click(player)
Event:FireServer()
end
local function Purchase(player)
MPS:PromptProductPurchase(player, 1139231250)
end
Die.MouseButton1Click:Connect(Purchase, Click)
My output says this error:
“MarketplaceService:PromptProductPurchase() player should be of type Player, but is of type nil”
I tried to look at the developer hub, but it just had a PlayerAdded function and a “player” parameter, which I tried making but failed.
It might be because my script is a localscript?
Any help/solutions?
None pass any arguments, just use player variable you already made
local MPS = game:GetService("MarketplaceService")
local Storage = game:GetService("ReplicatedStorage")
local Die = script.Parent.TextButton
local player = game.Players.LocalPlayer -- use this
local Event = Storage:WaitForChild("RemoteEvent")
local function Click()
Event:FireServer()
end
local function Purchase()
MPS:PromptProductPurchase(player, 1139231250)
end
Die.MouseButton1Click:Connect(Purchase, Click)
Players.PlayerAdded passes a player because that’s what the event does. It detects when a player joins and passes the player that joined as an argument
-- example
marketPlaceService.PromptPurchaseFinished:Connect(function(player, id, didPurchase)
-- "player" is the player that made the purchase
-- "id" is the product id of the purchase
-- "didPurchase" is a boolean value (true/false) that details if the player purchased it
if didPurchase then
print("player purchased product")
else
print("player did not purchase product")
end
end)
I did this, but it didn’t work, here’s what I wrote:
local MPS = game:GetService("MarketplaceService")
local Storage = game:GetService("ReplicatedStorage")
local Die = script.Parent.TextButton
local player = game.Players.LocalPlayer -- use this
local Event = Storage:WaitForChild("RemoteEvent")
local function Click()
Event:FireServer()
end
local function Purchase()
MPS:PromptProductPurchase(player, 1139231250)
end
Die.MouseButton1Click:Connect(Purchase, Click)
MPS.PromptPurchaseFinished:connect(function (Purchase)
-- Print all the details of the prompt, for example:
-- PromptPurchaseFinished PlayerName 11377306 true
print("PromptPurchaseFinished", Purchase)
end)
MPS.PromptPurchaseFinished:connect(function (player, id, Purchase)
-- Print all the details of the prompt, for example:
-- PromptPurchaseFinished PlayerName 11377306 true
print("PromptPurchaseFinished", player, id, Purchase)
end)
for it to print all the arguments, you have to turn it into a vararg (…)
MPS.PromptPurchaseFinished:connect(function (...) -- table containing all the information
-- Print all the details of the prompt, for example:
-- PromptPurchaseFinished PlayerName 11377306 true
print("PromptPurchaseFinished", ...)
end)
MPS.PromptPurchaseFinished:connect(function (player, id, Purchase)
print(player) -- prints the name of the player who did the purchase
print(id) -- prints the value of the id parameter. (the product id)
print(Purchase) -- prints either true or false (depending on whether or not they purchased it)
end)
MPS.PromptPurchaseFinished:connect(function (player, id, Purchase)
-- Print all the details of the prompt, for example:
-- PromptPurchaseFinished PlayerName 11377306 true
print("PromptPurchaseFinished")
print(player)
print(id)
print(Purchase)
end)
I also made an “id” variable containing the actual id