Weird issue with this script :

  1. What is the issue? Hello I have a problem with the script that you can find below. In fact in my game when a player makes a purchase, then if it is the first time that the player gives a value change and an event is triggered to offer the player a gift in the game. The problem is that some players who gave did not get this event (others got it, as if it was random) and I do not understand why. Could someone help me with this?
local Players = game:GetService('Players').PlayerAdded:Wait()
mps.ProcessReceipt = function(Player)
	if Players.Donator.Value == 0 then --PROBLEM START HERE
		Players.Donator.Value = 1
		game.ReplicatedStorage.DonationOK:FireClient(Players) 
	end -- PROBLEM FINISH HERE

	local amount = mps:GetProductInfo(Player.ProductId, Enum.InfoType.Product).PriceInRobux

	local success, err = pcall(function()
		local totalDonated = ods:GetAsync(Player.PlayerId) or 0
		ods:SetAsync(Player.PlayerId, totalDonated + amount)
	end)
	return success and Enum.ProductPurchaseDecision.PurchaseGranted or Enum.ProductPurchaseDecision.NotProcessedYet
end

If I understand correctly, you are only running this script to the player who most recently joined, because you are defining “Players” as

game:GetService(‘Players’).PlayerAdded

which means it defines Players as the player who joined most recently. To fix this, you should look into tables, which can be used to easily store and access data, or, in this case, all players that have joined.

Hello, thank you for your answer. Isn’t there an easier way to have all the players (but in a ‘local’ way) in a serverscript? Because I have no idea how to apply your advice.

Hey, this should work no ?

mps.ProcessReceipt = function(Player)
	for i, plr in pairs(game.Players:GetPlayers()) do
		if plr.Donator.Value == 0 then
			plr.Donator.Value = 1
			game.ReplicatedStorage.DonationOK:FireClient(players)
		end
	end
	local amount = mps:GetProductInfo(Player.ProductId, Enum.InfoType.Product).PriceInRobux

	local success, err = pcall(function()
		local totalDonated = ods:GetAsync(Player.PlayerId) or 0
		ods:SetAsync(Player.PlayerId, totalDonated + amount)
	end)
	return success and Enum.ProductPurchaseDecision.PurchaseGranted or Enum.ProductPurchaseDecision.NotProcessedYet
end

Before you run this you have to name the datastore and the value for donated because i did not get enough info.

local mps = game:GetService("MarketplaceService")
local dataStore = game:GetService("DataStoreService"):GetDataStore("dataStore") -- Edit name because i dont know the name of it

local function processReceipt(receiptInfo)
	local playerId = receiptInfo.PlayerId
	if playerId then
		
		local amount = receiptInfo.CurrencySpent -- Amount product costed to purchase
		local playerLeaderstats = game.Players:GetPlayerByUserId(playerId)
		
		local success, err = pcall(function()
			local newLeaderstat = playerLeaderstats.VALUENAMEHERE + amount -- On Valuenamehere, put the value name
			dataStore:SetAsync(playerId, newLeaderstat)
		end)
		
		return Enum.ProductPurchaseDecision.PurchaseGranted -- if the player leaves before they get the new value we can wait until they come back and have it
	else
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
end

mps.ProcessReceipt = processReceipt

I think so? Try testing it out.