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

Developer Product Problems?

Asked by 9 years ago

So I've been wrestling with Developer Products for a while and I'm pretty close to getting it to work, there's just one problem. I have multiple developer products that are supposed to give you credits after purchase. Only one of the buttons for the credit shop works, the rest do nothing, and they give no error.

I used this same scripts separately for the other buttons too.

The script that prompt's purchase is inside of the button while the script below is in Workspace.

local MarketplaceService = Game:GetService("MarketplaceService") local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory") local productId = 21113957 local numCredits = 750

game.Players.PlayerAdded:connect(function() print("start") local DeveloperProducts = game:GetService("MarketplaceService"):GetDeveloperProductsAsync():GetCurrentPage() for _, DevProductContainer in pairs(DeveloperProducts) do for Field, Value in pairs(DevProductContainer) do print(Field .. ": " .. Value) end print(" ") end print("end") end)

-- 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
            local credits = player:FindFirstChild("Data").Credits
            credits.Value = credits.Value + numCredits

            h = Instance.new("Hint", Workspace)
            h.Text = player.Name.." bought credits!"
            wait(5)
            h:Destroy()
        end
    end
end 

-- record the transaction in a Data Store
local playerProductKey = "p_" .. receiptInfo.PlayerId .. "_p_" .. receiptInfo.PurchaseId
ds:IncrementAsync(playerProductKey, 1)  

-- tell ROBLOX that we have successfully handled the transaction
return Enum.ProductPurchaseDecision.PurchaseGranted     

end

1 answer

Log in to vote
1
Answered by 9 years ago

The reason why only one works is because you use ProductId which product Id only equal's one Id, And it's price is set to only one price. If you want it too work, I recommend you do a table.

local ProductIds = {
IdHere = CreditsRewardedOnPurchase,
NextIdHere = CreditsRewardedOnPurchase,
}

-- Then in your Receipt script do the following.

for i,player in pairs(game.Players:GetPlayer()) do -- That get's all players in the game, Better than GetChildren, Because get children can fetch other things than players.
if player.userId  == receiptInfo.PlayerId then
for Id,Reward in pairs(ProductIds) do --Fetches the table of ProductIds, Which includes the Id and price.
if receiptInfo.ProductId == Id then
local Data = player:FindFirstChild("Data")
if Data then
local Credits = Data.Credits
Credits.Value = Credits.Value + Reward

local Hint = Instance.new("Hint", game.Workspace)
Hint.Text = player.Name.." bought "..Reward.." credits."
wait(5)
Hint:Destroy()
end
end
end
end

local playerProductKey = "p_" .. receiptInfo.PlayerId .. "_p_" .. receiptInfo.PurchaseId
ds:IncrementAsync(playerProductKey, 1) 
-- tell ROBLOX that we have successfully handled the transaction
return Enum.ProductPurchaseDecision.PurchaseGranted  
end
0
Do I put brackets around where it says "IdHere". I get an error if I dont. GlowingFlame 0 — 9y
0
Wait nevermind, I was. Also noticed a typo you made on line 8. GetPlayers() instead of GetPlayer(). Thanks alot for the help though, script is working fine now :). GlowingFlame 0 — 9y
0
Np, And you dont have to do brackets in the IDHere thing i meant to do IDHere == RewardHere, xImmortalChaos 565 — 9y
Ad

Answer this question