How could I make a purchase with my script? [OOP]

I am confused on how I could use robux to add money to my players leaderstat using OOP. I tried before hand and it was successful but had some errors. Here is my attempt at getting it to work.

local PlayerManager = require(script.Parent.Parent.Modules.PlayerManager)
local Market = game:GetService('MarketplaceService')
local Players = game:GetService('Players')



local Robux = {}
Robux.__index = Robux


function Robux.new(tycoon, part)
	local self = setmetatable({}, Robux)
	self.Tycoon = tycoon
	self.Instance = part
	self.DevTable = {self.Instance:GetAttribute('ID'), function(player) return self.Instance:GetAttribute('Cost') end}

	return self
end

function Robux:Init()
	self.Prompt = self:CreatePrompt()
	self.Prompt.Triggered:Connect(function(...)
		self:Press(...)
	end)
	self:EstablishProduct()
end

function Robux:CreatePrompt()
	local prompt = Instance.new("ProximityPrompt")
	prompt.HoldDuration = 0.5
	prompt.ActionText = self.Instance:GetAttribute("Display")
	prompt.ObjectText = "R$" .. self.Instance:GetAttribute("Cost")
	prompt.Parent = self.Instance
	return prompt
end


function Robux:Press(player)
	self:PromptPurchase(player)
end

function Robux:PromptPurchase(player)
	local id = self.Instance:GetAttribute('ID')
	
	if player == self.Tycoon.Owner then
		Market:PromptProductPurchase(player, id)
	end
end

function Robux:EstablishProduct()
	Market.ProcessReceipt = function(receiptInfo)
		local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
		if player and receiptInfo then
			local sucess = false
			local devProductId, func = self.DevTable[1], self.DevTable[2]
			if devProductId ~= nil and func ~= nil and type(func) == 'function' then
				if receiptInfo.ProductId == devProductId then
					local funcitonFinished = func(player)
					if funcitonFinished ~= true then
						warn('Something happened while trying to purchase item!')
						sucess = false
					else
						sucess = true
					end
				end
			end
		end
	end
end

return Robux

My issue is how to actually give the players what I want. For example, some cash or gems. Any help is appreciated.

Maybe don’t use OOP? It sounds like you’re trying to really force OOP into your game when you don’t actually need it. It’s just making your code grossly complex and unmaintainable for no reason.

It additionally brings along other problems, like the EstablishProduct method. Remember that ProcessReceipt is a definable callback and only one is allowed to exist. For every time you call EstablishProduct, you’re overwriting the ProcessReceipt callback and only the most recent call of it will actually get handled.

It’s not a complete solution but I’m thinking more in terms of the root problems than what you have now. Part of why you can’t figure out how to make purchases successfully with this code is because it’s the problem in the first place, mainly in respect to EstablishProduct overwriting ProcessReceipt every call and the unnecessary application of OOP (you don’t have stateful objects).

1 Like

My entire game system is run on OOP lol. But I think what you are saying is that I should not handle purchases using OOP?

You could think of it that way, but frankly I was saying that you shouldn’t be using OOP at all unless you actually have stateful objects. New developers tend to have a horrible habit of abusing OOP when they figure out how to start using it and it’s just simply not a good pattern in the majority of circumstances, including the one you’re dealing with. None of this is or needs to be stateful.

It’s not like it can’t be fixed as-is, you can still make your purchases work with OOP and I can give you advice on that if you really insist on keeping your code in OOP, it’s just a matter of shifting things around and thinking of your class in terms of a registry rather than a definer and that’d be an actual solution to the problem but at heart it’s just entirely unnecessary to OOP the majority of your code.

1 Like

Yeah I get what you are saying. This project for me was kinda just getting the basics of OOP and when the right time to be using such a system as OOP. [Thanks for help btw]