Basic Scripting Help

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?

2 Likes

You have a player variable, but for the Purchase and Click functions aren’t passing it through when you use Connect.

2 Likes

MouseButton1Click doesn’t pass any arguments

1 Like

So I have to use something like “MouseButton1Down” instead of “MouseButton1Click”?

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

2 Likes

Wait, I got 1 more question. How do I check if they bought it, then do something?

For example, if they bought the “nuke” developer product, how would I check if they actually bought it, and not just clicked the button?

1 Like

Use prompt product finished event

-- 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)
1 Like

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)

it passes 3 arguments, not one: change it to

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)
1 Like

There’s a red underline under my id, 1139231250

can you send a screen shot of it, there’s most likely a syntax error

Screen Shot 2021-01-08 at 12.35.45 PM

There was an underline though, the screenshot didn’t show /:

the “id” parameter is suppose to be the name of the parameter that contains the value, not the value itself.
Change it back to

MPS.PromptPurchaseFinished:connect(function (player, id, Purchase)
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)
1 Like

There were no errors, but nothing printed either.

what does your script look like now

1 Like
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

alright, you made the test studio purchase right?

If so, try using :Connect instead of :connect since :connect was deprecated a while ago

MPS.PromptPurchaseFinished:Connect(function (player, id, Purchase)
1 Like

I replaced “connect” with “Connect”, but it still doesn’t print