I need constructive feedback on my product handler script

I need constructive feedback on my product handler script, It’s been a while I scripted developer products.

local MarketPlaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local Ids = {
	
	["50 Points"] = 1034505260,
	["110 Points"] = 1034505524,
	["250 Points"] = 1034505858,
	["600 Points"] = 1034506135,
	["1500 Points"] = 1034506492,
}

local function productHandler(productInfo)
	
	local Player = Players:GetPlayerByUserId(productInfo.PlayerId)
	
	if not Player then return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if productInfo.ProductId == Ids["50 Points"] then
		Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 50
		
	elseif productInfo.ProductId == Ids["110 Points"] then
	    Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 110
		
	elseif productInfo.ProductId == Ids["250 Points"] then
		Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 250
		
	elseif productInfo.ProductId == Ids["600 Points"] then
	    Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 600
		
	elseif productInfo.ProductId == Ids["1500 Points"] then
        Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 1500
	end
	
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketPlaceService.ProcessReceipt = productHandler
1 Like

The if statements are a bit inefficient and the Ids table is useless, instead you could have a dictionary with the IDs as the keys and the number of points as the values and just add the value onto the player’s points by doing player.leaderstats.Points.Value += points[productInfo.ProductId]

Result:

local MarketPlaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local pointQuantities = {
	
	[1034505260] = 50,
	[1034505524] = 110,
	[1034505858] = 250,
	[1034506135] = 600,
	[1034506492] = 1500,
}

local function productHandler(productInfo)
	
	local Player = Players:GetPlayerByUserId(productInfo.PlayerId)
	
	if not Player then return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	Player.leaderstats.Points.Value += pointQuantities[productInfo.ProductId]
	
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketPlaceService.ProcessReceipt = productHandler

Hope this helped.

1 Like

I would use a table of functions with the index of the id instead.

productArray[id] = function(productInfo, playerObject)

This makes it a lot cleaner cause you can handle entire products with different functionality just by adding a function to that specific id. So say you wanted to handle more than just adding points creating functions for each individual would benefit.

You could also make a handler to detect if the id in question are to be given by points as well. This could make it a lot easier so it’s not a bunch of copy and paste and change the points given which can always get messy as we all know.

+= is a new compound operator ROBLOX just recently added. It means + the current value by (number) so += 1 is equal to value = value + 1

Then you’re just taking more time to do so. That’s how it used to be done but I’ve gotten attached to the compound operators already. Can easily just do Player.leaderstats.Points.Value += 110

1 Like

This line of code is indexing pointQuantities with the ProductId, which will return the corresponding number of points to be given(pointQuantities[1034505260] returns 50). One of the main advantages with dictionaries is that you’re able to sort of connect two values and access one with the other.

Hope this helped.