Hello, I’m currently trying to figure out how to properly combine my Developer Products handler with a trolling system.
Right now I have a global system where players can spectate others and see options like:
- Kill
- Kick
- Kill All
- Kick All
When a player clicks one of these options, they are prompted to purchase a Developer Product. After purchase, the action should be executed (e.g. kill the target player, or affect the whole server if it’s a “global” action).
The problem
I’m not sure how to properly pass the target context into my global Developer Products handler.
- If a player buys a targeted troll action (like “kill this specific player”), I need to pass that target player into the handler.
- If it’s a global action (like “kill all”), then there should be no specific target passed.
But my current system is a global handler using MarketplaceService.ProcessReceipt, so I don’t know how to attach or retrieve the target player data for each purchase.
local DeveloperProductsHandler = {}
local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local PurchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
local productsById = {}
local processingCache = {}
function DeveloperProductsHandler:Register(productId: number, handler)
assert(type(productId) == "number", "ProductId must be a number.")
assert(type(handler) == "function", "Handler must be a function.")
assert(not productsById[productId], ("Handler already registered for product ID %d."):format(productId))
productsById[productId] = handler
end
function DeveloperProductsHandler:Init()
MarketplaceService.ProcessReceipt = function(receiptInfo)
local purchaseId = receiptInfo.PurchaseId
local productId = receiptInfo.ProductId
local playerId = receiptInfo.PlayerId
local purchaseKey = "Receipt_" .. purchaseId
if processingCache[purchaseId] then
return Enum.ProductPurchaseDecision.PurchaseGranted
end
local alreadyProcessed = false
local success, err = pcall(function()
PurchaseHistoryStore:UpdateAsync(purchaseKey, function(old)
if old and old.processed == true then
alreadyProcessed = true
return old
end
return {
processed = false,
productId = productId,
playerId = playerId,
timestamp = os.time()
}
end)
end)
if not success then
warn("[DevProducts] DataStore error:", err)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
if alreadyProcessed then
processingCache[purchaseId] = true
return Enum.ProductPurchaseDecision.PurchaseGranted
end
local player = Players:GetPlayerByUserId(playerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
local handler = productsById[productId]
if not handler then
warn("No handler for product:", productId)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
local grantSuccess, result = pcall(function()
return handler(player, purchaseId)
end)
if not grantSuccess then
warn("Handler error:", result)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
if result ~= true then
warn("Purchase not completed (handler returned false)")
return Enum.ProductPurchaseDecision.NotProcessedYet
end
local saveSuccess, saveErr = pcall(function()
PurchaseHistoryStore:UpdateAsync(purchaseKey, function(old)
return {
processed = true,
productId = productId,
playerId = playerId,
timestamp = os.time()
}
end)
end)
if not saveSuccess then
warn("Failed to finalize receipt:", saveErr)
return Enum.ProductPurchaseDecision.PurchaseGranted
end
processingCache[purchaseId] = true
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
return DeveloperProductsHandler