Re-purchase doesn't work

Look I have a problem, when I buy money once they are credited to the player, when the second time I buy then nothing happens, that is, the money to the player is not credited but at the same time the purchase is successful in what could be the problem?


local MarketplaceService = game:GetService("MarketplaceService") 
local DatastoreService = game:GetService("DataStoreService") 

local PreviousPurchases = DatastoreService:GetDataStore("PreviousPurchases")

local productFunctions = {} 

local cash5000 = 1823229791 -- 5000 cash id --
local cash15000 = 1823230874 -- 15000 cash id --
local cash50000 = 1823230999 -- 50000 cash id --

local function giveCash(plr, amt) 
	wait(1)
	plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + amt
    game.ServerStorage:WaitForChild("PlayerMoney"):FindFirstChild(plr.Name).Value = game.ServerStorage:WaitForChild("PlayerMoney"):FindFirstChild(plr.Name).Value + amt
end

productFunctions[cash5000] = function(info, plr) -- gives plr 5000 cash
	giveCash(plr, 5000)
end

productFunctions[cash15000] = function(info, plr) -- gives plr 15000 cash
	giveCash(plr, 15000)
end

productFunctions[cash50000] = function(info, plr) -- gives plr 50000 cash
	giveCash(plr, 50000)
end


local function procRec(reciept)


	local ID = reciept.PlayerId.."-"..reciept.PurchaseId 

	if PreviousPurchases:GetAsync(ID) then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	local player = game.Players:GetPlayerByUserId(reciept.PlayerId)
	if not player then return Enum.ProductPurchaseDecision.NotProcessedYet end
	local handler = productFunctions[reciept.ProductId]
	local s, e = pcall(function()
		handler(reciept, player)
	end)

	if s then print("supposedly went through") return Enum.ProductPurchaseDesicion.PurchaseGranted elseif not s then warn(e) return Enum.ProductPurchaseDecision.NotProcessedYet end
end
MarketplaceService.ProcessReceipt = procRec
1 Like

Are you using gamepasses or developer products?

I’m using Developer Products for money

is this a local or server script?

Do you have other scripts that process that same marketplace function? Do have any errors??

No there are no other scripts, no errors, the first time the purchase passes and the money is credited to the leaderboard, the second time I buy the purchase passes the money is not there.

this script is server, not local.

there’s a missing PlayersService variable, you should create one

Where do I need to use it in the script?

first, call for Players service under MarketplaceService

I tried it doesn’t work I don’t know what the problem is

local MarketplaceService = game:GetService("MarketplaceService") 
local DatastoreService = game:GetService("DataStoreService") 

local PreviousPurchases = DatastoreService:GetDataStore("PreviousPurchases")

local productFunctions = {} 

local cash5000 = 1823229791 -- 5000 cash id --
local cash15000 = 1823230874 -- 15000 cash id --
local cash50000 = 1823230999 -- 50000 cash id --

local function giveCash(plr, amt) 
	wait(1)
	plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + amt
    game.ServerStorage:WaitForChild("PlayerMoney"):FindFirstChild(plr.Name).Value = game.ServerStorage:WaitForChild("PlayerMoney"):FindFirstChild(plr.Name).Value + amt
end

productFunctions[cash5000] = function(info, plr) -- gives plr 5000 cash
	giveCash(plr, 5000)
end

productFunctions[cash15000] = function(info, plr) -- gives plr 15000 cash
	giveCash(plr, 15000)
end

productFunctions[cash50000] = function(info, plr) -- gives plr 50000 cash
	giveCash(plr, 50000)
end

try separating these lines of code into a localScript and keep your procRec function in server script