Product not Getting Granted

Hello developers, I am currently scripting a developer product system which should grant the player necessary rewards when it gets bought.
However, as the following function runs, it only grants the first products reward and not the others. The problem is, the function doesn’t pass the second and the other if statements and it should. Here is the script. Note: There is no issue with the prompts. Also, the first product gets granted too.

local MarketPlaceService = game:GetService("MarketplaceService")

productIds = {
	id1, id2, id3, id4, id5
}


local function giveProductReward(reward, player)
	player:WaitForChild("leaderstats"):WaitForChild("Money").Value += reward
end


MarketPlaceService.ProcessReceipt = function(receiptInfo)
	
	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
	
	
	for _, productIdFromTable in pairs(productIds) do
		if receiptInfo.ProductId == productIdFromTable and player then
			
			if productIdFromTable == id1 then
				giveProductReward(10000, player)
				return Enum.ProductPurchaseDecision.PurchaseGranted 
			elseif productIdFromTable == id2 then
				print("second prodct")
				giveProductReward(30000, player)
				return Enum.ProductPurchaseDecision.PurchaseGranted 
			elseif productIdFromTable == id3 then
				giveProductReward(100000, player)
				return Enum.ProductPurchaseDecision.PurchaseGranted 
			elseif productIdFromTable == id4 then
				giveProductReward(500000, player)
				return Enum.ProductPurchaseDecision.PurchaseGranted 
			elseif productIdFromTable == id5 then
				giveProductReward(1000000, player)
				return Enum.ProductPurchaseDecision.PurchaseGranted 
			end
			
			
		else
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
	end
end
1 Like

For this thing, i would advice you using table statements, it’s powerfull combo of If and Tables that allow you turn all of those statements into one or two, i made tutorial about it here: Scripting Manual

if you read about tables, then:

local productArray = {
  someId1 = Coins,
  someId2 = Gems,
  someId3= Money
}

-- code

-- function 

if productArray[productId] then
  giveProductReward(productId) -- in my case gives specific value, you can change it to numbers of money
end

Unfortunately, we can’t approach the problem this way because all of my reward types are same, only the values are different. Also, the following lines cause an error.

productIds = {
	1880674645 = 10000, -- id and the reward. This causes an error. ...
}
1 Like

it’s not problem, use brackets to enter any string

{
["3891318831"] = 10000,
}

This would be an ideal solution for another project. However, this basically doesn’t help for my script and the system I use.

1 Like

if statements can block each other when this one simply checks for correct product

Yes, that is basically the problem. Actually, the main problem is on the first if statement I think. I am currently trying to fix it.

1 Like

I now understand the issue and I can’t believe we couldn’t recognize it. If you look at the else statement, if the player purchases an item with an index of 2 or more, the for loop stops running because of the return statement.
Basically, I put the return statement after the for loop finishes looping and now everything works fine.

1 Like

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