Does anyone know how to make a currency increase on devproduct purchase? Ive looked everywhere but cant find anything good
Example:
--Pseudocode
if plr.Buy(devproduct) then
plr.Money += 100
end
Does anyone know how to make a currency increase on devproduct purchase? Ive looked everywhere but cant find anything good
Example:
--Pseudocode
if plr.Buy(devproduct) then
plr.Money += 100
end
The way basically everyone does it, which could maybe be why you had a hard time learning to use it, is by using MarketplaceService and ProcessReceipt, however there are other highly discouraged ways to do it like promptpurchase events, which have no backup in case a transaction fails, leading to scam situations.
So would
local MarketplaceService = game:GetService("MarketplaceService")
MarketplaceService.PromptPurchaseFinished:Connect(function(player, assetId, isPurchased)
if isPurchased then
print(player.Name .. " bought an item with AssetID: " .. assetId)
else
print(player.Name .. " didn't buy an item with AssetID: " .. assetId)
end
end)
be discouraged?
Yeah, theres basically no safety on what would happen if the purchase goes through and but the sale fails
alr here we go.
This is a server script
local MPS = game:GetService("MarketplaceService")
local id = 0000 -- DEV PRODUCT ID HERE
MPS.ProcessReceipt = function(data)
if data.ProductId == id then
local plr_id = data.PlayerId
local plr = game.Players:GetPlayerByUserId(plr_id)
if plr then
plr.leaderstats.Coins.Value += 100 -- change to the path to the value
return Enum.ProductPurchaseDecision.PurchaseGranted -- tell roblox purchase went through
else
return Enum.ProductPurchaseDecision.NotProcessedYet -- if the player leaves during the purchase, we need to tell roblox to fire this event when the plr rejoins
end
end
end
@anon84578691
if u have a question feel free to ask
Could i put this inside the āclick to purchaseā script or is that a no-no? Also, what does it do when you write āreturn ā¦ā?
just make it a server script in serverscript service
itāll tell roblox that the purchase went through.
it would mean its possible for your player to purchase an item, but receive nothing, and be forced to purchase again. While if something went wrong with the processreceipt method it would keep retrying to give the purchased item to the player until it confirms that they received it.
it is as common as how common it is for a server error to ocurr. While it may not be often, the fact that it can happen should be enough
Is the return thing necessary?
YES YES YES IT IS
if we dont then the player will continue to get free coins
also just give me the solution already
Bro. Youāre missing so many things inside your server script. You didnāt properly check for and record purchases using a GlobalDataStore.
You also didnāt return Enum.ProductPurchaseDecision.PurchaseGranted
when the transaction is successfully completed or if itās detected that the purchase has already been granted using the āPurchaseHistoryā data store.
@anon84578691 Use these scripts instead:
Place this code in a local script under a button that a player can click/press:
local MarketplaceService = game:GetService("MarketplaceService")
local button = script.Parent
local productId = 000000 -- place developer product ID here
local cooldown = false
button.MouseButton1Click:Connect(function()
if cooldown == false then
cooldown = true
MarketplaceService:PromptProductPurchase(game.Players.LocalPlayer, productId)
task.wait(1.5)
cooldown = false
end
end)
Then make a server script, place it in ServerScriptService, and put the code below. The code below is a sample from the Developer Hub.
local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
local productFunctions = {}
--- Replace the 000000 with your product ID
productFunctions[000000] = function(receipt, player)
-- Logic/code for player buying 100 Coins (may vary)
local leaderstats = player:FindFirstChild("leaderstats")
local Coins = leaderstats and leaderstats:FindFirstChild("Gold")
if Coins then
Coins.Value =+ 100
-- Indicate a successful purchase
return true
end
end
local function processReceipt(receiptInfo)
-- Determine if the product was already granted by checking the data store
local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
local success, isPurchaseRecorded = pcall(function()
return purchaseHistoryStore:UpdateAsync(playerProductKey, function(alreadyPurchased)
if alreadyPurchased then
return true
end
-- Find the player who made the purchase in the server
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
-- The player probably left the game
-- If they come back, the callback will be called again
return nil
end
local handler = productFunctions[receiptInfo.ProductId]
local success, result = pcall(handler, receiptInfo, player)
-- If granting the product failed, do NOT record the purchase in datastores.
if not success or not result then
error("Failed to process a product purchase for ProductId:", receiptInfo.ProductId, " Player:", player)
return nil
end
-- Record the transcation in purchaseHistoryStore.
return true
end)
end)
if not success then
error("Failed to process receipt due to data store error.")
return Enum.ProductPurchaseDecision.NotProcessedYet
elseif isPurchaseRecorded == nil then
-- Didn't update the value in data store.
return Enum.ProductPurchaseDecision.NotProcessedYet
else
-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
-- Set the callback; this can only be done once by one script on the server!
MarketplaceService.ProcessReceipt = processReceipt
I did add that
also theres not need to put it in a datastore. Thatās just extra work. My script included everything.
Incorrect. Using a DataStore is crucial for this, since it properly checks for and records purchase.
well I added the purchase granted. Thereās not need to say that I didnāt add that when I clearly did
I donāt see a DataStore inside your script there.
Iām assuming the OP took your script already, so this doesnāt matter anyway.
Iām not attacking you. Iām just telling educating you that your script can sometimes fail to check and record properly.
well I dont think itās overall necessary to record purchases. The OP asked how to give currency after purchase, not record transactions. Thereās no need to spin it off like my script wont work.
wait so I thought doing it in a datastore was to see when someone purchased something. I guess I was wrong. But canāt enum.PurchaseGranted do the same thing?