Problem with a ModuleScript

Hi, I want to create a module script to simplify the process of prompting players with developer products. I’ve attempted to find solutions on the DevForum, but none have been helpful.

The error i have is this:

ModuleScript

local Product = {}
Product.__index = Product

function Product.getPlayerFromId(id)
	for i,v in pairs(game.Players:GetChildren()) do
		if v.userId == id then
			return v
		end
	end
	return nil
end

function Product.new(productID,customFunction)
	
	
	local MarketplaceService = game:GetService("MarketplaceService")

	
	

	MarketplaceService.ProcessReceipt = function(receiptInfo)
		local data = setmetatable({},Product)
		data.productID = receiptInfo.ProductId
		data.playerId = receiptInfo.PlayerId
		data.player = Product.getPlayerFromId(data.playerId)

		

		if data.productID == productID then
			
			return customFunction
		end
			
			
	
		return Enum.ProductPurchaseDecision.PurchaseGranted, data		
		
	end
end


function Product:PromptProduct()
	local MarketplaceService = game:GetService("MarketplaceService")
	MarketplaceService:PromptProductPurchase(self.playerId, self.productID )
end

return Product



Script

-- Main script


local devModule = require(game.ReplicatedStorage.DevProductsModules)

function PrintSomething()
	print("you bought this thing")
end

local product = devModule.new(1640596424, PrintSomething)

product:PromptProduct()

Any help will be appreciated.

A huge flaw in your ModuleScript is that you connect a new function to .ProcessReciept everytime you want a new product, but .ProcessReciept can only be connected to 1 function at a time. You might want to fix that before fixing other bugs.

I’m sorry, but I lack knowledge on how to handle developer products and their functions, such as receipts. Could you please guide me on how to resolve this issue?

I would set a .ProcessReciept outside the ModuleScript functions that handles all products. You also can just use :GetPlayerByUserId instead of making a custom function.

Code (may not work as intended):

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

local productFunctions = {}

local Product = {}
Product.__index = Product

function Product.new(productID: number, customFunction)
	assert(productID and customFunction, "Missing parameters")
	assert(type(productID) == "number", "productId is not a number")
	
	local self = {}
	self.productID = productID
		
	productFunctions[productID] = customFunction
	
	return setmetatable(self, Product)
end

function Product:PromptProduct(player: Player)
	assert(typeof(player) == "Instance" and player:IsA("Player"), "player is not a valid player instance")

	MarketplaceService:PromptProductPurchase(player, self.productID)
end

MarketplaceService.ProcessReceipt = function(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, 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

return Product

Example use case:

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

--//Modules
local Product = require(ReplicatedStorage.Product)

--//Variables
local cashProduct = Product.new(0000000, function(player)
	player.leaderstats.Cash.Value += 100
	
	return true
end)

--//Functions
Players.PlayerAdded:Wait()
cashProduct:PromptProduct(Players:GetPlayers()[1])

Thank you for your efforts. Unfortunately, I encountered an error message: ‘ReplicatedStorage.DevProductsModules:22: userId is not a number - Server - DevProductsModules:22.’" in this line of the code.

assert(type(userId) == "number", "userId is not a number")

I forgot :PromptProductPurchase takes a player instead of a user id. It should work now.

I’m very sorry, but I’m still encountering errors in the code you provided.
Screenshot_3

I used the code in the ServerScript like this:

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

--//Modules
local Product = require(ReplicatedStorage.DevProductsModules)

function printSomething()
	print("you bought this product")
end

--//Variables
local cashProduct = Product.new(1640596424,printSomething())

--//Functions
Players.PlayerAdded:Wait()
cashProduct:PromptProduct(Players:GetPlayers()[1])

You didn’t return true in your function. You also don’t have the player argument in your function. Also, can you just use the code I made to make sure it works? You can create your own custom functions after we actually make the module work.

1 Like

Im really sorry for that. Your code works perfectly fine and It works perfectly fine now that i added the argument and the return true. Thank you for your help and patience

1 Like

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