Developer Products Bought

So, I am making a script that when you buy a small pile of “bobux” it gives you ten. I made this script for it.
local MarketplaceService = game:GetService(“MarketplaceService”)

local function processReceipt(receiptInfo)
local player = game:GetService(“Players”):GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
–player does not exist / left game
return Enum.ProductPurchaseDecision.NotProcessedYet
end
print(player.Name … "just bought " … receiptInfo.ProductId)
player.leaderstats.bobux.Value = player.leaderstats.bobux.Value + 10

return Enum.ProductPurchaseDecision.PurchaseGranted

end

MarketplaceService.ProcessReceipt = processReceipt

Here is where the problem lies. I am also making a medium pile that gives you 75 bobux. How would I be able to recognize which one is being bought, and give the bobux accordingly.

local MarketplaceService = game:GetService(“MarketplaceService”)

local function processReceipt(receiptInfo)
local player = game:GetService(“Players”):GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
–player does not exist / left game
return Enum.ProductPurchaseDecision.NotProcessedYet
end
print(player.Name … "just bought " … receiptInfo.ProductId)
player.leaderstats.bobux.Value = player.leaderstats.bobux.Value + 75

return Enum.ProductPurchaseDecision.PurchaseGranted

end

MarketplaceService.ProcessReceipt = processReceipt

1 Like

ProcessReceipt passes the product Id to the function, whenever its called, just have a table that has the information about the Product Id, and then check how much reward you wanna give from it, for example like this:

local devProductRewards = {
    [1249571274] = { Reward = 50 },
    [2157128591] = { Reward = 100 }
}

local function processReceipt(receiptInfo)
   local boughtProductInfo = devProductRewards[receiptInfo.ProductId] --This is where you get info from the table
   print(boughtProductInfo.Reward) --Should print reward, if the id was found in the table.
end

MarketplaceService.ProcessReceipt = processReceipt

Also whenever you ask for code help, please format the code properly using ``` at top and end of code block

Alright, will do that next time, I knew there was a way to do that, but I forgot. Thank you! Have a nice day/night if it is nighttime for you rn.

2 Likes

I would put that at the beginning of the code?

1 Like

Yeah you do it like this

image

One quick question, how would I make it give out the rewards according to what reward the player has bought.

1 Like

Don’t you mean like for suppose the player bought the developer product for 75 Bobux, which ID was for example: 124128571, and then you would want to give the reward according to the product Id.

And thats exactly what I gave the example for in my last post, above. You might want to read through what it does again :slight_smile:

bobux :grinning::+1:

In all seriousness, this is a great script. I’m not that advanced of a Lua scripted to give tips on improvements, but I think you did an amazing job.

Thank you for the compliment! I am not the best scripter either, but practice makes perfect. BOBUX

3 Likes

Hmmmmmmm. I still can’t figure out how I would be able to give the rewards accordingly. Like on the leaderstats would I do something like

player.leaderstats.bobux.Value = player.leaderstats.bobux.Value +reward

I am still confused.

1 Like

Okay, I don’t like to spoon feed but as this is seeming hard to you, I will do it and explain what I am doing.

So heres the example:

local devProductRewards = {
    [1249571274] = { Reward = 50 }, --Here the 1249571274 is the key (just use product ID), which you access to get the Reward which is 50, so you can make an idea whats going in here I believe.
    [2157128591] = { Reward = 100 } --Just like the above, this is same, except its for a different ID and reward.
}

local function processReceipt(receiptInfo)
   local boughtProductInfo = devProductRewards[receiptInfo.ProductId] --This is where you get info from the table, using the product id as key as explained earlier
   if not boughtProductInfo return end --Break the thread if product's info isn't found in the reward table

player.leaderstats.bobux.Value = player.leaderstats.bobux.Value + boughtProductInfo.Reward --This will get the Reward value from the table of the Key you got above
end

MarketplaceService.ProcessReceipt = processReceipt

If you are still having confusion with how the Table’s work and stuff, I highly recommend you try to learn them from the API reference. Also read comments through the code :slight_smile:

Ok. Thank you for going out of your way to help me. Very much appreciated.

1 Like

IT WORKS! Very much appreciated.

1 Like

Its alright, we’re always here at developer forum to help you out :slight_smile: Also I would recommend you to learn more about the tables & stuff, as it seems you’re new to it

1 Like

Yeah, I just recently began my coding adventure :smile:

2 Likes