Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 10 years ago

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

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

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)

0
Thank you! TheImmuneNoobKiller 0 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

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

0
Oops, my bad. Try now Shawnyg 4330 — 10y
0
I still get this error when I click: MarketplaceService:PromtProductPurchase() player should be a type of Player, but is of type nil TheImmuneNoobKiller 0 — 10y

Answer this question