Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I make the user receive the item after the purchase?

I'm new to developer products and stuff. I made a developer product that gives the user ingame currency. The script works, but the user gets the currency before you press "buy". In the wiki it tells me to use datastores to keep track of who bought the developer product, why would that be necessary?

Script:

script.Parent.MouseButton1Click:connect(function()
	local Market = game:GetService("MarketplaceService")
	local id = 23747241 -- the Developer Product ID
	Market:PromptProductPurchase(game.Players.LocalPlayer, id)
	print("Bought")
	game.Players.LocalPlayer.Clashbux.Value = game.Players.LocalPlayer.Clashbux.Value + 5000
end)

1 answer

productId = 0 -- change to your id

script.Parent.MouseButton1Click:connect(function()
	game:GetService("MarketplaceService"):PromptProductPurchase(game.Players.LocalPlayer, productId)
end)

local MarketplaceService = Game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
 

MarketplaceService.ProcessReceipt = function(receiptInfo) 
 
	-- find the player based on the PlayerId in receiptInfo
	for i, player in ipairs(game.Players:GetChildren()) do
		if player.userId == receiptInfo.PlayerId then
 

			if receiptInfo.ProductId == productId then
 
-- I'm guessing Clashbux is in leaderstats
				player.leaderstats.Clashbux.Value = player.leaderstats.Clashbux.Value + 5000
			end
		end
	end	
 
	local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.PurchaseId
	ds:IncrementAsync(playerProductKey, 1)	
 
	return Enum.ProductPurchaseDecision.PurchaseGranted		
end

KEY POINTS: receiptinfo records the purchase after receipt is processed you are able to put your next move under it the last part tells server that you handled the purchase with any other questions ask me

4 comments

0

Thanks, but I would like it if you acutally EXPLAINED what the heck is going on in this script.

0

I don't understand a thing.

0

Sorry I forgot explain when I answered

0

Clashbux isnt in leaderstats. LocalPlayer finds the player and in order to get into leaderstats you gotta do "game.Players.LocalPlayer.leaderstats.Clashbux"

This discussion is preserved as it was. New answers, comments, and votes are closed.