Alright so, I'm trying to make it so players can buy products to get more Cash. However, It's not working. It's not giving the players what they need. Although the datastore seems to be tracking purchases but it wont give players anything still. Here's the code, yes I used the code from ROBLOX, which did not WORK for me.
local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
-- Data store for tracking purchases that were successfully processed
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
-- Table setup containing product IDs and functions for handling purchases
local productFunctions = {}
-- ProductId for 450$
productFunctions[1141809709] = function(receipt, player)
print"wow"
--[[wait(1)
local statss = player:FindFirstChild("leaderstats")
local gold = statss:FindFirstChild("Cash")
if gold then
gold.Value = gold.Value + 450
-- Indicate a successful purchase
return true
end --]]
end
-- ProductId for 900$
productFunctions[1141809771] = function(receipt, player)
print"wow"
-- Logic/code for player buying 100 gold (may vary)
--[[local statss = player:FindFirstChild("leaderstats")
local gold = statss:FindFirstChild("Cash")
if gold then
gold.Value = gold.Value + 900
-- Indicate a successful purchase
return true
end --]]
end
-- The core 'ProcessReceipt' callback function
local function processReceipt(receiptInfo)
-- Determine if the product was already granted by checking the data store
local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
local purchased = false
local success, errorMessage = pcall(function()
print"successful"
purchased = purchaseHistoryStore:GetAsync(playerProductKey)
end)
-- If purchase was recorded, the product was already granted
if success and purchased then
print"gg"
return Enum.ProductPurchaseDecision.PurchaseGranted
elseif not success then
error("Data store error:" .. errorMessage)
end
-- Find the player who made the purchase in the server
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
-- The player probably left the game
-- If they come back, the callback will be called again
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- Look up handler function from 'productFunctions' table above
local handler = productFunctions[receiptInfo.ProductId]
-- Call the handler function and catch any errors
local success, result = pcall(handler, receiptInfo, player)
if not success or not result then
warn("Error occurred while processing a product purchase")
print("\nProductId:", receiptInfo.ProductId)
print("\nPlayer:", player)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- Record transaction in data store so it isn't granted again
local success, errorMessage = pcall(function()
print"succeess"
purchaseHistoryStore:SetAsync(playerProductKey, true)
end)
if not success then
error("Cannot save purchase data: " .. errorMessage)
end
-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
return Enum.ProductPurchaseDecision.PurchaseGranted
end
-- Set the callback; this can only be done once by one script on the server!
MarketplaceService.ProcessReceipt = processReceipt
Thanks in advance.
