Market is fixed but rebroken

So in my last topic: How can I fix this?, I fixed the market script and was able to buy packs/cash/crystals. BUT, a new bug sprouted and now it gives you stats from the last thing you bought (if you buy crystals then buy cash twice you’ll get crystals twice).
Here is my script:

--// Services
local Market = game:getService("MarketplaceService")

local moneyFolder = game.ServerStorage:WaitForChild("PlayerMoney")

--// Functions
local function CompletePurchase(PurchaseInfo)
	local plr = game:GetService("Players"):GetPlayerByUserId(PurchaseInfo.PlayerId)
	local PurchaseTable = {
		--cash tiers
		[966463278] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 10000
			end
		},
		[966463330] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 26250
			end
		},
		[966463421] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 82500
			end
		},
		[966463466] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 172500
			end
		},
		[966463511] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 600000
			end
		},
		[966463536] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 1250000
			end
		},
		[966463586] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 3240000
			end
		},
		[966463613] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 6000000
			end
		},
		--crystals tiers
		[967342219] = {
			Callback = function(player)
				local stats = player:WaitForChild("leaderstats").Crystals
				stats.Value = stats.Value + 45
			end
		},
		[967342248] = {
			Callback = function(player)
				local stats = player:WaitForChild("leaderstats").Crystals
				stats.Value = stats.Value + 118
			end
		},
		[967342261] = {
			Callback = function(player)
				local stats = player:WaitForChild("leaderstats").Crystals
				stats.Value = stats.Value + 371
			end
		},
		[967342277] = {
			Callback = function(player)
				local stats = player:WaitForChild("leaderstats").Crystals
				stats.Value = stats.Value + 743
			end
		},
		[967342311] = {
			Callback = function(player)
				local stats = player:WaitForChild("leaderstats").Crystals
				stats.Value = stats.Value + 2700
			end
		},
		[967342329] = {
			Callback = function(player)
				local stats = player:WaitForChild("leaderstats").Crystals
				stats.Value = stats.Value + 5625
			end
		},
		[967342343] = {
			Callback = function(player)
				local stats = player:WaitForChild("leaderstats").Crystals
				stats.Value = stats.Value + 14580
			end
		},
		[967342355] = {
			Callback = function(player)
				local stats = player:WaitForChild("leaderstats").Crystals
				stats.Value = stats.Value + 27000
			end
		},
		--limited offers
		[967561226] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 26250000
			end
		},
		[967561248] = {
			Callback = function(player)
				local stats = player:WaitForChild("leaderstats").Crystals
				stats.Value = stats.Value + 1181250
			end
		},
		--packs
		[970778723] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 5000
				local stat2 = player:WaitForChild("leaderstats").Crystals
				stat2.Value = stat2.Value + 150
			end
		},
		[971060036] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 4000
				local stat2 = player:WaitForChild("leaderstats").Crystals
				stat2.Value = stat2.Value + 80
			end
		},
		[971060069] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 100000
				local stat2 = player:WaitForChild("leaderstats").Crystals
				stat2.Value = stat2.Value + 180
			end
		},
		[971060091] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 1500000
				local stat2 = player:WaitForChild("leaderstats").Crystals
				stat2.Value = stat2.Value + 350
			end
		},
		[971110628] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 15000000
				local stat2 = player:WaitForChild("leaderstats").Crystals
				stat2.Value = stat2.Value + 1250
			end
		},
		[970778707] = {
			Callback = function(player)
				local stats = moneyFolder:WaitForChild(player.Name)
				stats.Value = stats.Value + 15000
				local stat2 = player:WaitForChild("leaderstats").Crystals
				stat2.Value = stat2.Value + 200
			end
		},
	}
	
	for key, tab in pairs(PurchaseTable) do
		if PurchaseInfo.ProductId == key then
			tab.Callback(plr)
			break
		end
	end
	
end

Market.ProcessReceipt = CompletePurchase

Sorry for the very long script!

What solutions, if any, have you tried prior to posting this here? It looks like you just went from one thread to another without doing any of your own work.

I mean, before coming here I tried some things with this chunk of code here:

At first I didn’t have break but then I added that to see if it worked which it didn’t.

I don’t think putting return at the end of the function will do anything, right?

I’ve simplified your code below, you don’t even need to iterate over the keys in the dictionary, you can index it directly with the ProductID. Also, I’m not sure if you’ve declared this somewhere else, but be sure to return an Enum for the ProductPurchaseDecision, like in my example below.

local product_table = Purchase_Table[PurchaseInfo.ProductId]
local state, error_msg = pcall(function()  product_table.Callback(plr)  end)
if state then --//If the function runs without error
return Enum.ProductPurchaseDecision.PurchaseGranted
else
return Enum.ProductPurchaseDecision.NotProcessedYet
end

Hope this helps :slight_smile:

1 Like

I think I forgot the ProductPurchaseDecision, I think that’s what is up!

EDIT: It works now. How stupid of me!

1 Like