Having an issue with devproducts

So I have a system where user can buy an item with either robux or in-game currency.
Screenshot 2023-08-26 at 10.45.10 PM

The problem im having now is how to effectivly handle a purchase on the server:

replicatedStorage.OnPurchaseRequest.OnServerInvoke = function(Player, RobuxOrCash, ProductID)
	if RobuxOrCash == "Robux" then
		--Robux
		MarketplaceService:PromptProductPurchase(Player, ProductID)
		--then in the ProcessReceipt event I process the purchase?
	else
		--Cash
		Player.leaderstast.Cash -= ProductPrice
		--Then I create a second whole script to handle the purchase here????
	end

end

So basically with this system, if I wanted to change something with how the item is purchased I would have to edit BOTH functions, it seems too ineffecient to be the only way :thinking:

1 Like

This seems to work, however for this purpose you do not need to send any data back to the client, so just use a RemoteEvent.

But anyway, It seems fine, and a valid way to manage this, however a couple of things:

  1. Yes you use ProcessReceipt to Manage Purchases of a Developer Product, this will only apply if they are spending robux, but if the purchase was successful, just fire the exact same function that is used to give the item, the callback will give you the receipt, so you then have to check to make sure the information is correct to give the correct item, or use a preset table to grab objects based on its Id, which is more cleaner than multiple if and elseif statements.

  2. Because the Player is purchasing using the In-game currency, just check if they have sufficient funds to buy the item, and if not, dont allow them to buy it, if they have enought, fire a function to give them the item.

All you really need to do on the Server is to check whether the information is correct, and do something depending on the outcome. (ex: give or not give the item), I would only Recommend one script to handle purchases instead of two, to keep organization.

make sure that the ProductPrice is not negative, it is a a whole integer, and is not NaN ProductPrice ~= ProductPrice.

You’re correct in identifying that having separate code for each type of purchase can lead to duplication and potential issues when maintaining and updating your game. To improve efficiency and organization, you can consider centralizing the purchase logic while abstracting the details of each payment method. One way to do this is by using a modular approach with separate functions for each type of payment.

Here’s how you could structure your server code to achieve this:

luaCopy code

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

local function processRobuxPurchase(Player, ProductID)
    -- Handle Robux purchase logic
    MarketplaceService:PromptProductPurchase(Player, ProductID)
end

local function processCashPurchase(Player, ProductPrice)
    -- Handle in-game currency purchase logic
    Player.leaderstast.Cash -= ProductPrice
    -- Add more logic as needed, e.g., giving the player the item
end

ReplicatedStorage.OnPurchaseRequest.OnServerInvoke = function(Player, RobuxOrCash, ProductID, ProductPrice)
    if RobuxOrCash == "Robux" then
        processRobuxPurchase(Player, ProductID)
    else
        processCashPurchase(Player, ProductPrice)
    end
    -- You can add more shared processing logic here if needed
end

With this approach, you separate the concerns of each payment method into its own function. This allows you to modify and maintain each payment method’s logic independently. If you need to make changes or improvements, you only need to edit the specific function related to that payment method.

By modularizing your code like this, you improve maintainability, readability, and organization, making it easier to manage different payment methods without duplicating code unnecessarily.

but this is going to run asynchronously, if it yeilded and returned weather the purchase was succ or fail then i woulda done this

(I gave this project a 2 day break to prepare for school and this solution randomly popped into my head)

1 Like

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