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

How do I add ingame cash when clicking a gui? [Still not working]

How would I give 5000 cash to someone when they click the gui? I have got this

--LocalScript in StarterPack
-- setup local variables
local buyButton = game.StarterGui.q.Frame.TextButton
local productId = 20518668
 
-- when player clicks on buy brick prompt him/her to buy a product
buyButton.MouseButton1Click:connect(function()
	game:GetService("MarketplaceService"):PromptProductPurchase(game.Players.LocalPlayer, productId)
end)
buyButton.MouseButton1Down:connect(function()
 
 
 
 
--Script in BuyButton part
-- setup local variables
local MarketplaceService = Game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
local productId = 20518668
 
-- define function that will be called when purchase finished
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
 
 
			-- check which product was purchased
			if receiptInfo.ProductId == productId then
 
				-- handle purchase
				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 5000
			end
		end
	end	
 
	-- record the transaction in a Data Store
	local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.PurchaseId
	ds:IncrementAsync(playerProductKey, 1)	
 
	-- tell ROBLOX that we have successfully handled the transaction
	return Enum.ProductPurchaseDecision.PurchaseGranted		
end

2 answers

Local script inside of the TextButton.

id = 20518668 --dev product id

script.Parent.MouseButton1Click:connect(function()
	p = game.Players.LocalPlayer
	game:GetService("MarketplaceService"):PromptProductPurchase(p, id)
	local MarketplaceService = Game:GetService("MarketplaceService")
	local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
	MarketplaceService.ProcessReceipt = function(receiptInfo) 
		if p.userId == receiptInfo.PlayerId then
			if receiptInfo.ProductId == id then
				p.leaderstats.Cash.Value = p.leaderstats.Cash.Value + 5000
			end
		end	
		
	-- record the transaction in a Data Store
	local playerProductKey = receiptInfo.PlayerId .. "_got_" .. receiptInfo.PurchaseId
	ds:IncrementAsync(playerProductKey, 1)	
	
	-- tell ROBLOX that we have successfully handled the transaction
	return Enum.ProductPurchaseDecision.PurchaseGranted		
	end
end)

1 comment

0

Thank you!

There is still a problem it wont do anything when I press it.

2 comments

0

Oops, my bad. Try now

0

I still get this error when I click: MarketplaceService:PromtProductPurchase() player should be a type of Player, but is of type nil

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