Inventory duplicates?

So I have this crate you can open to get a random tower for robux but theres an issue. Sometimes, it will give me a bunch of the tower I got? Heres the script:

local function process(receiptInfo)
			local playerwhoSummoned = Players:GetPlayerByUserId(receiptInfo.PlayerId)

			if not playerwhoSummoned then
				return Enum.ProductPurchaseDecision.NotProcessedYet
			end
						
			if receiptInfo.ProductId == 1636004071 then
				local chosenRarity = getInfectedRarity()

				for i, rosterTower in pairs(allTowers["Exclusive"]) do
					if rosterTower.Name == chosenRarity then
						table.insert(data[playerwhoSummoned.UserId].OwnedTowers,rosterTower.Name)
						print(data[playerwhoSummoned.UserId],rosterTower.Name)
						print(rosterTower)
						infectedEvent:FireClient(playerwhoSummoned,rosterTower)
					end
				end
			end
		end
		MarketService.ProcessReceipt = process

Any help is appreciated, thanks!

1 Like

Ok so the issue is I think it chooses multiple times even though I only purchased once, how do I solve this?

You’re never returning when the .ProcessReciept has been successful. You should follow Roblox’s example to avoid this.

Code:

--//Services
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")

--//Tables
local productFunctions = {}

productFunctions[1636004071] = function(receipt, player)
	local chosenRarity = getInfectedRarity()

	for i, rosterTower in pairs(allTowers.Exclusive) do
		if rosterTower.Name == chosenRarity then
			table.insert(data[player.UserId].OwnedTowers,rosterTower.Name)
			print(data[player.UserId],rosterTower.Name)
			print(rosterTower)
			infectedEvent:FireClient(player,rosterTower)
		end
	end
	
	return true
end

--//Functions
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

You also might have to add a break in the middle of the loop once a tower has been chosen.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.