Hello. I have four dev products for my game, but only one of them works for some reason.
Here is one of my scirpts, but I have 3 more that just have different ID’s and stuff but they do not work.
Why is this happening?
local DataStoreService = game:GetService("DataStoreService")
local LeaderstatDataStore = DataStoreService:GetDataStore("LeaderstatDataStore")
local DeveloperProductID = 1597899398 -- Change this ID with your Developer Product ID
game.Players.PlayerAdded:Connect(function(player)
local Leaderstat = player.leaderstats.FairyDust
local LeaderstatKey = player.UserId .. "FairyDust"
local success, savedValue = pcall(function()
return LeaderstatDataStore:GetAsync(LeaderstatKey)
end)
if success and savedValue then
Leaderstat.Value = savedValue
end
local Purchased = false
game:GetService("MarketplaceService").ProcessReceipt = function(receiptInfo)
if receiptInfo.PlayerId == player.UserId and receiptInfo.ProductId == DeveloperProductID and not Purchased then
Purchased = true
Leaderstat.Value = Leaderstat.Value + 250
local Success, error = pcall(function()
LeaderstatDataStore:SetAsync(LeaderstatKey, Leaderstat.Value)
end)
if not Success then
warn("Failed to save leaderstat data:", error)
end
Purchased = false
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end)
Well first of all you are defining the ProcessReceipt callback every time a player joins. Define it only once.
Try something like this instead (this is from a roblox tutorial)
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local productFunctions = {}
-- ProductId 123123 brings the user back to full health
productFunctions[123123] = function(receipt, player)
if player.Character and player.Character:FindFirstChild("Humanoid") then
player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth
-- Indicate a successful purchase
return true
end
end
-- ProductId 456456 awards 100 gold to the user
productFunctions[456456] = function(receipt, player)
local stats = player:FindFirstChild("leaderstats")
local gold = stats and stats:FindFirstChild("Gold")
if gold then
gold.Value = gold.Value + 100
return true
end
end
local function processReceipt(receiptInfo)
local userId = receiptInfo.PlayerId
local productId = receiptInfo.ProductId
local player = Players:GetPlayerByUserId(userId)
if player then
-- Get the handler function associated with the developer product ID and attempt to run it
local handler = productFunctions[productId]
local success, result = pcall(handler, receiptInfo, player)
if success then
-- The user has received their benefits!
-- return PurchaseGranted to confirm the transaction.
return Enum.ProductPurchaseDecision.PurchaseGranted
else
warn("Failed to process receipt:", receiptInfo, result)
end
end
-- the user's benefits couldn't be awarded.
-- return NotProcessedYet to try again next time the user joins.
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- Set the callback; this can only be done once by one script on the server!
MarketplaceService.ProcessReceipt = processReceipt