Adding Donations ServerSide Not Working

Hey I’m trying to add peoples Donation into a leaderstat and this script isn’t working after they purchase one of the buttons. If anybody can find the issue that would be amazing!

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

local donationIds = {
	"1265665801",
	"1265665829", 
	"1265665853", 
	"1265665881", 
	"1265665924",
}
MPS.ProcessReceipt = function(receiptInfo)
	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then 
		return Enum.ProductPurchaseDecision.NotProcessedYet 
	end
	
	if table.find(donationIds, receiptInfo.PurchaseId) then
		local amountDonated = receiptInfo.CurrencySpent
		player.leaderstats.Donated.Value += amountDonated

		return Enum.ProductPurchaseDecision.PurchaseGranted 
	end
end

0 errors in the output aswell and it needs to add how much they just donated to the game into the leaderstats

  • game.Players should be Players .

Here is the corrected code:

local MPS = game:GetService(“MarketplaceService”)
local Players = game:GetService(“Players”)

local donationIds = {
“1265665801”,
“1265665829”,
“1265665853”,
“1265665881”,
“1265665924”,
}
MPS.ProcessReceipt = function(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end

if table.find(donationIds, receiptInfo.PurchaseId) then
local amountDonated = receiptInfo.CurrencySpent
player.leaderstats.Donated.Value += amountDonated

  return Enum.ProductPurchaseDecision.PurchaseGranted 

end
end

1 Like

Hey man I appreciate the help but it still doesn’t work when I test in studio, do I need to test in roblox? But heres the script updated.

local MPS = game:GetService('MarketplaceService')
local Players = game:GetService('Players')

local donationIds = {
	'1265665801',
	'1265665829',
	'1265665853',
	'1265665881',
	'1265665924'
}
MPS.ProcessReceipt = function(receiptInfo)
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	if table.find(donationIds, receiptInfo.PurchaseId) then
		local amountDonated = receiptInfo.CurrencySpent
		player.leaderstats.Donated.Value += amountDonated

		return Enum.ProductPurchaseDecision.PurchaseGranted 
	end
end

There is no error in the code I provided. It looks like the code is setting up a custom ProcessReceipt function for the MarketplaceService , which is called when a player makes a purchase through the Roblox marketplace. The function checks if the purchase ID is in the donationIds table, and if it is, it adds the amount of currency spent on the purchase to the player’s Donated leaderboard statistic. If the purchase ID is not in the donationIds table or if the player cannot be found, the function returns Enum.ProductPurchaseDecision.NotProcessedYet .

1 Like

I am using that exact code and the IDs are on point so there must be a slight error.

I put a else Print(“error”) and it seems that the code messes up in this if statement

I think instead of PurchaseId you should use ProductId, in which case you have to remove the ’ symbols in the table to make the id’s number values.

local MPS = game:GetService('MarketplaceService')
local Players = game:GetService('Players')

local donationIds = {
	1265665801,
	1265665829,
	1265665853,
	1265665881,
	1265665924
}
MPS.ProcessReceipt = function(receiptInfo)
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	if table.find(donationIds, receiptInfo.ProductId) then
		local amountDonated = receiptInfo.CurrencySpent
		player.leaderstats.Donated.Value += amountDonated

		return Enum.ProductPurchaseDecision.PurchaseGranted 
	end
end
1 Like

Oh it’s becuse Table.find was removed in like 2017, try this:

local MPS = game:GetService('MarketplaceService')
local Players = game:GetService('Players')

local donationIds = {
	'1265665801',
	'1265665829',
	'1265665853',
	'1265665881',
	'1265665924'
}
MPS.ProcessReceipt = function(receiptInfo)
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	local donation = table.findValue(donationIds, receiptInfo.PurchaseId)
	if donation then
		local amountDonated = receiptInfo.CurrencySpent
		player.leaderstats.Donated.Value += amountDonated

		return Enum.ProductPurchaseDecision.PurchaseGranted 
	end
end
1 Like

You can use the table.findIndex function to search for a value in a table and get its index, or you can use the table.findValue function to search for a value in a table and get the value itself.

1 Like

neither of those methods exist, table.find has always been a thing

1 Like

this is what I ended up getting with ur code

I will try this instead thank you very much!

Not a response, but I just got the ability to reply :blush:

1 Like

I added a bunch of print statements with ur code and this is what I got when I donated 10 robux.

local MPS = game:GetService('MarketplaceService')
local Players = game:GetService('Players')

local donationIds = {
	1265665801,
	1265665829,
	1265665853,
	1265665881,
	1265665924
}
MPS.ProcessReceipt = function(receiptInfo)
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		print("Not a play purchasing")
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	if table.find(donationIds, receiptInfo.ProductId) then
		print("found Donation id in table")
		local amountDonated = receiptInfo.CurrencySpent
		print(amountDonated)
		player.leaderstats.Donated.Value += amountDonated
		print("Added leaderstats")

		return Enum.ProductPurchaseDecision.PurchaseGranted 
	else 
		print("error")
	end
end


so the problem appears to be it doesn’t know the amount donated

reffering to this post, when you purchase somethign in studio it will always be zero. So it should work in-game

1 Like

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