The problem with this script, is that it gives you 5 swords before you make the purchase, and another 5 when you do. I want it so when you buy it, it gives you a sword, but when you die/respawn, you have to buy it again.
-- setup local variables
local MarketplaceService = Game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
local productId = 20550096
tool = game.Lighting:findFirstChild("Sword")
-- define function that will be called when purchase finished
MarketplaceService.ProcessReceipt = function(receiptInfo)
-- find the player based on the PlayerId in receiptInfo
for i, player in ipairs(game.Players:GetChildren()) do
if player.userId == receiptInfo.PlayerId then
-- check which product was purchased
if receiptInfo.ProductId == productId then
-- handle purchase: In this case, we are giving the player a sword.
game.Lighting.Sword:clone().Parent = player.Backpack
game.Lighting.Sword:clone().Parent = player.StarterGear
end
end
-- record the transaction in a Data Store
local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.PurchaseId
ds:IncrementAsync(playerProductKey, 1)
-- tell ROBLOX that we have successfully handled the transaction
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
|