I am trying to develop a Cash shop that will award the player cash when buy they buy whatever product they choose. I have gotten the GUI and GUI scripts down, meaning it offers the product when they click it. But I can’t find a effective way to award the player the cash. I have tried many YouTube tutorials but none of them seem to work. Thank you.
This might work
(If you’re using leaderstats)
game.players.PlayerAdded:Connect(function(Player)
-- leaderstats stuff
if purchaseditem then-- replace with your way of detecting if they bought it
Player.leaderstats.currency.Value += -- your increment
end
end)
Sorry this is a little late but how would I detect it through a dev product? I’m late because I was busy with stuff.
You would do this by changing the ProcessReceipt callback in MarketplaceService:
local marketplaceService = game:GetService("MarketplaceService")
local addAmounts = {
[MyProductId] = AmountOfCashToAdd
}
local function onPurchased(receiptInfo)
local amountToAdd = addAmounts[receiptInfo["ProductId"]]
local plrId = receiptInfo["PlayerId"]
add(plrId, amountToAdd)
return Enum.ProductPurchasingDecision.PurchaseGranted
end
marketplaceService.ProcessReceipt = onPurchased
If you need any more help, feel free to ask. I typed this in the little text box thing, so it might not be 100% accurate, but it gets the idea across.
ProcessReceipt docs: MarketplaceService.ProcessReceipt
Devproduct docs: Developer Products – In‑Game Purchases
Edit: I forgot one very important thing: Always return Enum.ProductPurchasingDecision.PurchaseGranted in your callback.
Using developer products is a little annoying, so i will do my best to explain it.
Inside of a script in ServerScriptService, you will want to have the line of code to prompt the dev product purchase. The client will use a RemoteEvent to tell the server they are purchasing the product, i do not know if this is needed though, you might be able to prompt it through the client.
MarketplaceService:PromptProductPurchase(Player, ProductID)
Then, you will want to have a function in the same script, maybe a different one.
MarketplaceService.ProcessReceipt = function(receiptInfo)
if not Players:GetPlayerByUserId(receiptInfo.PlayerId) then return end
if receiptInfo.ProductId == ProductID then
-- Reward with cash here
end
end
Your script should end up looking something like this:
Before replying to a post you should try reading it first.
Why would a remote be necessary for this? You can prompt purchases from the client.
Im still learning about developer products myself, and that is how i did it on my game, with my very minimal knowledge.
Oh I gotcha. I like to think of developer products like they’re remote events which fire when a player purchase a product.
When I buy it it doesn’t give the money. Is there another step I have to do? Sorry for the late responses I’ve been busy with many things. @tacovaIk
Show me your script pls, I don’t know how you’re handling money in your game
Pretty much all I have is when they click to buy the product all it does is prompts the purchase. I have no scripts to give them the money after buying it that’s what I need help with.
Alright, I gotcha. Here’s how I would do this:
local marketplaceService = game:GetService("MarketplaceService")
local dataStoreService = game:GetService("DataStoreService")
local currencyDataStore = dataStoreService:GetDataStore("Currency")
local players = game:GetService("Players")
--Your cash adding amounts paired with their corresponding dev product id go here.
local cashToAdd = {
[1315656213] = 10 --This saves us from making a long else/if chain if you have multiple products
}
local function getCashFromDatastore(plr) --This loads cash data
local cash
local success, err = pcall(function()
cash = currencyDataStore:GetAsync(plr.UserId)
end)
if err then
print(err)
end
if success then
if cash == nil then
return 0
else
return cash
end
end
return -1
end
local function updateCashDatastore(plr) --This saves cash data
local success, err = pcall(function()
local cash = plr:FindFirstChild("Cash")
if cash.Value >= 0 then
currencyDataStore:SetAsync(plr.UserId, cash.Value) --Won't save if there was an error retrieving the player's cash earlier
end
end)
if err then
print(err)
end
end
local function onAdded(plr) --A way to store the cash of a player in the game
local cash = Instance.new("IntValue")
cash.Name = "Cash"
local value = getCashFromDatastore(plr)
cash.Value = value
cash.Parent = plr
end
local function onPurchase(receiptInfo) --receiptInfo is what the ProcessReceipt callback in marketplaceService returns
local cashAmount = cashToAdd[receiptInfo["ProductId"]]
local plr = players:GetPlayerByUserId(receiptInfo["PlayerId"])
local intValue = plr:FindFirstChild("Cash")
intValue.Value += cashAmount --Adds the cash
return Enum.ProductPurchaseDecision.PurchaseGranted --It's necessary to return this so Roblox knows you've given the player what they've purchased
end
marketplaceService.ProcessReceipt = onPurchase
players.PlayerAdded:Connect(onAdded)
players.PlayerRemoving:Connect(updateCashDatastore)
If you have any questions about the script be sure to let me know. I tried to add comments to make it more readable. This goes on the server btw.
If you add a line in your script that says:
MarketplaceService:PromptProductPurchase(Player, ProductId)
if Enum.ProductPurchaseDecision.PurchaseGranted then
player.leaderstats.Cash.Value += -- value
end