I think you did something wrong in the stored datas. You probably add a solidier everytimes the player join because you saved he bought the solidier with robux.
I had this same issue, I resolved it by creating a table in my DataStore and each developer product purchase gives you a unique ID that you can save to your DataStore and when you are processing the receipt, you can check if the players data table already contains that unique Id, if it does, do not give them what they already received.
Or just when the player buy a solidier, save his solidier amount with one more (and before he leave, add his solidier). When the player rejoin, he will get the good amount of solidiers.
Dear all this is my code… thanks for the time taken to help . .
local productFunctions = {}
productFunctions[XXX] = function(receipt, player)
add marine code
end
productFunctions[YYY] = function(receipt, player)
add combat engineer code
end
local function processReceipt(receiptInfo)
local handler = productFunctions[receiptInfo.ProductId]
local success, result = pcall(handler, receiptInfo, receiptInfo.PlayerId)
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
return Enum.ProductPurchaseDecision.PurchaseGranted
You are saving it because it will repeat the purchase. Let’s say you have bought it once, and want to buy a product again. The script then thinks it is the same ID, and uses that to repeat the purchase. However, you will not receive the robux from the second one! (this have I personally tried out).
One way to fix this is by creating a table in your datastore. For every purchase within this product, creates an unique ID.
i fixed up the code. However it seems to be failing at the pcall for the handler . . i always get not success although the soldier was spawned successfully. This results in the sales not saving into datastore
local DS = game:GetService(“DataStoreService”)
local MarketplaceService = game:GetService(“MarketplaceService”)
local purchaseHistoryStore = DS:GetDataStore(“PurchaseHistory”)
local productFunctions = {}
productFunctions[1085506706] = function(receipt, player)
Code to give a soldier
return true
end
productFunctions[1085506610] = function(receipt, player)
Code to give a repair man
return true
end
productFunctions[1085506705] = function(receipt, player)
Code to reload turrets
return true
end
local function processReceipt(receiptInfo)
local playerProductKey = receiptInfo.PlayerId … “_” … receiptInfo.PurchaseId
local purchased = false
local success, errorMessage = pcall(function()
purchased = purchaseHistoryStore:GetAsync(playerProductKey)
end)
if success and purchased then
return Enum.ProductPurchaseDecision.PurchaseGranted
elseif not success then
error(“Data store error:” … errorMessage)
end
local player = game:GetService(“Players”):GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
local handler = productFunctions[receiptInfo.ProductId]
local success, result = pcall(handler, receiptInfo, receiptInfo.PlayerId)
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
local success, errorMessage = pcall(function()
purchaseHistoryStore:SetAsync(playerProductKey, true)
end)
if not success then
error("Cannot save purchase data: " .. errorMessage)
end
return Enum.ProductPurchaseDecision.PurchaseGranted
i fixed up the code. However it seems to be failing at the pcall for the handler . . i always get not success although the soldier was spawned successfully. This results in the sales not saving into datastore
local DS = game:GetService(“DataStoreService”)
local MarketplaceService = game:GetService(“MarketplaceService”)
local purchaseHistoryStore = DS:GetDataStore(“PurchaseHistory”)
local productFunctions = {}
productFunctions[1085506706] = function(receipt, player)
Code to give a soldier
return true
end
productFunctions[1085506610] = function(receipt, player)
Code to give a repair man
return true
end
productFunctions[1085506705] = function(receipt, player)
Code to reload turrets
return true
end
local function processReceipt(receiptInfo)
local playerProductKey = receiptInfo.PlayerId … “_” … receiptInfo.PurchaseId
local purchased = false
local success, errorMessage = pcall(function()
purchased = purchaseHistoryStore:GetAsync(playerProductKey)
end)
if success and purchased then
return Enum.ProductPurchaseDecision.PurchaseGranted
elseif not success then
error(“Data store error:” … errorMessage)
end
local player = game:GetService(“Players”):GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
local handler = productFunctions[receiptInfo.ProductId]
local success, result = pcall(handler, receiptInfo, receiptInfo.PlayerId)
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
local success, errorMessage = pcall(function()
purchaseHistoryStore:SetAsync(playerProductKey, true)
end)
if not success then
error("Cannot save purchase data: " … errorMessage)
end
return Enum.ProductPurchaseDecision.PurchaseGranted
Just make the “PlayerProductKey” the player’s UserId and then you can use GetAsync to get their data. I also suggesting using a table inside your datastore called Purchases or something and then do table.insert(myDataStore[playerUserId].Purchases, transactionId) then you can use table.find(table, value) to see if they have already gotten the product before.