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

Does Anybody Know Why This Script Isn't Working?

Asked by 9 years ago

This script is supposed to add whoever bought the developers product add "Money" to the leaderboard. I got this script from the roblox wiki and edited it a little

--LocalScript in StarterPack -- setup local variables local buyButton = game.StarterGui.Donations.DonationAmounts.Donate50Tx local productId = 20806015

-- 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)

--Script in BuyButton part -- setup local variables local MarketplaceService = Game:GetService("MarketplaceService") local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory") local productId = 20806015

-- 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.Money.Value = player.leaderstats.Money.Value + 50
        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

0
btw it is not in a local script and it not in the gui that you buy it from Rayquazaj 0 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

Hello. I currently take this as you wish to ProcessReceipt(). You also stated that you got this from the Wiki article. So I will base my answer off of that.

I have a game that uses the same script that is on the wiki, so my suggestion is, leave it as unedited as possible.

local MarketplaceService = game:GetService("MarketplaceService")
local HealthID, GoldID = 11111, 22222
local PurchaseHistory = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")

MarketplaceService.ProcessReceipt = function(receiptInfo) 
    local playerProductKey = receiptInfo.PlayerId .. ":" .. receiptInfo.PurchaseId
    if PurchaseHistory:GetAsync(playerProductKey) then
        return Enum.ProductPurchaseDecision.PurchaseGranted --We already granted it.
    end
    -- find the player based on the PlayerId in receiptInfo
    for i, player in ipairs(game.Players:GetPlayers()) do
        if player.userId == receiptInfo.PlayerId then
            -- check which product was purchased (required, otherwise you'll award the wrong items if you're using more than one developer product)
            if receiptInfo.ProductId == HealthID then
                -- handle purchase. In this case we are healing the player.
                player.Character.Humanoid.Health = 100
            elseif receiptInfo.ProductId == GoldID then
                -- handle purchase. In this instance we're giving the player 100 extra gold. 
                player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + 100
            end
        end 
    end
    -- record the transaction in a Data Store
    PurchaseHistory:SetAsync(playerProductKey, true)    
    -- tell ROBLOX that we have successfully handled the transaction (required)
    return Enum.ProductPurchaseDecision.PurchaseGranted     
end

Let's break this code down piece by piece for a better understanding of what each piece means.

local MarketplaceService = game:GetService("MarketplaceService") Is as simple as can be, it requests the MarketplaceService, which is required for anything using R$ or TIX.

local HealthID, GoldID = 11111, 22222 This is what is very easy once you learn it (if you don't already know what it means). The "HealthID" is 11111 and the "GoldID" is 22222 so if you wish to set this up for your own items you should set the line up as: local item1,item2,item3 = 11111, 22222, 33333 the numbers should be replaced by your Developer Product IDs. The item names can be changed to whatever, just do not name two things the same.

The rest is really just Lua jibberish and unneeded for your question. So we will go down to the next part that matters.

if receiptInfo.ProductId == HealthID then If we use my example from above, this would say if receiptInfo.ProductId == item1 then the line under that would give something such as extra health, or faster walkspeed. But instead of doing end we will do something else... elseif So, our next product should be elseif receiptInfo.ProductId == item2 then then give them another powerup or whatever you wish for them to have. If you wish to have more, continue doing elseif for your next item(s).

Please note: You do not have to use this for multiple purchases if you do not have multiple products. You just don't have to put any elseif or anymore local items. Please be sure to use this one script for any and all developer products. This script will prevent you from having a bunch of the same scripts clogging up your ServerScriptService (which is by the way where this is supposed to go) and it helps you find the needed product quicker.

I hope this helped you!

0
Also, this should be in a server script (not LocalScript or ModuleScript) TheStudentPilot 75 — 9y
Ad
Log in to vote
-1
Answered by 9 years ago

should i put this script in the workspace

Answer this question