I was making a script that would, once bought, give a player +100 coins. For some reason, it won’t work for me. It will prompt me to buy the developer product, but when I test buy it, it doesn’t do anything. Here is the script. (The parent of the script is a TextButton)
local id = "1006738178"
local localplayer = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
game.MarketplaceService:PromptProductPurchase(localplayer, id)
if Enum.ProductPurchaseDecision.PurchaseGranted == true then
game.Players.LocalPlayer.leaderstats.Coinn.Value = game.Players.LocalPlayer.leaderstats.Coinn.Value + 1
elseif Enum.ProductPurchaseDecision.PurchaseGranted == false then
game.Players.LocalPlayer.leaderstats.Coinn.Value = game.Players.LocalPlayer.leaderstats.Coinn.Value + 0
end
end)
Please tell me what is wrong with this script and how to fix it. Thanks!
I’m not the best with DevProducts but heres what I used in one of my projects
local MarketPlaceService = game:GetService("MarketplaceService")
local function processReceipt(receiptInfo)
if receiptInfo.ProductId == 'productID' then
local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
player.leaderstats.'leaderstat'.Value = player.leaderstats.'leaderstat'.Value + 'Value'
return Enum.ProductPurchaseDecision.PurchaseGranted
end
MarketPlaceService.ProcessReceipt = processReceipt
I forgot, in the button you must add a LocalScript
local MarketPlaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local productId ='productid'
script.Parent.MouseButton1Down:Connect(function(promptPurchase)
local player = Players.LocalPlayer
MarketPlaceService:PromptProductPurchase(player, productId)
end)
This doesn’t give the player the currency, only prompts the purchase. The other code controls the if purchased part
EDIT: Have this script in a LocalScript inside the button
and Have the other one somewhere in ServerScriptService
Okay, but when I put the 1st script you typed in the ServerScriptService, am I supposed to put that in a LocalScript? Because you had a local value that was local player = game.Players.LocalPlayer, and only LocalScript can have LocalPlayer.
Here is the script that I have right now inside ServerScriptService under a regular script:
local player = game.Players.LocalPlayer
local productId = 1006738178
local function processReceipt(receiptInfo)
if receiptInfo.ProductId == 1006738178 then
local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
player.leaderstats.Coinn.Value = player.leaderstats.Coinn.Value + 100
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
Here’s the one that I have in the TextButton under a LocalScript:
local MarketPlaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local productId = 1006738178
script.Parent.MouseButton1Down:Connect(function(promptPurchase)
local player = Players.LocalPlayer
MarketPlaceService:PromptProductPurchase(player, productId)
end)
Do you mean just add local MarketPlaceService = game:GetService("MarketplaceService") to the script in serverscriptservice? If so, this did not work for me.
Developer Products are handled using a Callback. That callback is MarketPlaceService.ProcessReceipt(). That callback is supposed to return Enum.ProductPurchaseDecision.PurchaseGranted when the purchase is granted and if it has not yet been granted return Enum.ProductPurchaseDecision.NotProcessedYet
Your script did not work for me. I tried modifying it a bit, but it was no use–
local MarketPlaceService = game:GetService("MarketplaceService")
local player = game:GetService("Players").LocalPlayer
local productId = 1006738178
local assetId = 1006738178
script.Parent.MouseButton1Down:Connect(function()
MarketPlaceService:PromptProductPurchase(player, productId)
end)
game.MarketplaceService.PromptPurchaseFinished:Connect(function(player, assetId, ispurchased)
if ispurchased then
player.leaderstats.Coinn.Value = player.leaderstats.Coinn.Value + 100
else
player.leaderstats.Coinn.Value = player.leaderstats.Coinn.Value + 0
end
end)
You are not supposed to use PromptProductPurchaseFinished for handling purchases. This function is commonly used only for seeing if the player has bought or did not buy the developer product. You are to use ProcessReceipt.
The developer hub page documenting PromptProductPurchaseFinished advises you to not use this function for handling purchases
I’m sorry, @LukaDev_0, I’m a bit new to scripting, and have no clue about how to use processReciept. Should I do something like this?
MarketPlaceService.ProcessReceipt()
if Enum.ProductPurchaseDecision.PurchaseGranted then
player.leaderstats.Coinn.Value = player.leaderstats.Coinn + 100
elseif Enum.ProductPurchaseDecision.NotProcessedYet then
return
end