Coins not given on Dev Product Purchase

So I’m trying to achieve recieving Coins on a Dev Product Purchase.

Everything works fine but when I purchase the Product it doesn’t print out anything nor give me the Coins.

I tried changing everything with the player, but nothing works.

Here’s the script:

local marketplaceService = game:GetService("MarketplaceService")
local productID = 1274134792
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	marketplaceService.ProcessReceipt = function(recieptInfo)
		local playerPurchasing = players:GetPlayerByUserId(recieptInfo.PlayerId)
		if not playerPurchasing then
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
		if recieptInfo.ProductId == productID then
			print("Purchased!")
			player.Coins.Value += 50
		end
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end)
1 Like

Try to see if one of these could help

  1. No.
  2. Not the thing I’m looking for.
  3. Still no.
  4. Again, no.
  5. Not the thing I’m looking for.

Try this

local marketplaceService = game:GetService("MarketplaceService")
local productID = 1274134792
local players = game:GetService("Players")

marketplaceService.ProcessReceipt = function(recieptInfo)
	local playerPurchasing = players:GetPlayerByUserId(recieptInfo.PlayerId)
	if not playerPurchasing then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	if recieptInfo.ProductId == productID then
		print("Purchased!")
		playerPurchasing.Coins.Value += 50
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

You have incorrectly used the marketplace service.

Remove the “Process Receipt” function outside of Players.PlayerAdded - this one function will handle all players. Amendments to the code are as follows:


local marketplaceService = game:GetService("MarketplaceService")
local productID = 1274134792
local players = game:GetService("Players")

marketplaceService.ProcessReceipt = function(recieptInfo)
	local playerPurchasing = players:GetPlayerByUserId(recieptInfo.PlayerId)
	if not playerPurchasing then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	if recieptInfo.ProductId == productID then
		print("Purchased!")
		player.Coins.Value += 50
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

Review the marketplace function here for more information:
https://developer.roblox.com/en-us/api-reference/callback/MarketplaceService/ProcessReceipt

As to why it likely hasnt worked before:

Chances are your player joined before the script loaded, hence its always good practice to do this:

players.PlayerAdded:Connect(myFunction)

for _, player in ipairs(game.Players:GetPlayers()) do
	myFunction(player)
end

As far as I’m aware, scripts load before anything else (after their parent loads).
Your example would be more fitting for a client script.

Actually, the scripts do not load before anything else - it all comes down to device speeds etc. If a player is slower at connecting then chances are the script will have fully loaded and the player wouldn’t have been classed as “joined”.

For me, as my PC is exceptionally fast I have found on numerous occasions that in low intensive games my player has joined before the listener has been added.

Its important to note however that this usually only ever occurs with the first player, any consecutive players beyond this will load as normal.

Seeming as the listener hadn’t been created by the time the player actually did join, the listener did not pick up previous players.

I’ve only ever noticed this happening in solo test mode, and I honestly don’t even see that happening anymore. It would be based on connection speed as well, if anything. The reverse is the only thing I’ve found true currently (scripts loading before anything else and requiring to wait for the required instances to have loaded before interacting with them).

Your code is also dangerous as it will likely run the function twice. Once when the player joins, and a second time when iterating through the players (if the player has loaded by the time that part of the code is executed).

So from what I understand, I need to put the script in a function, make a for loop and then call the function on PlayerAdded. Am I correct?

I just tested it on my client now, a single millisecond in delay on my side causes for my player instance to join before the playeradded catches it (in a completely empty workspace).

No, in your case you just need to remove the PlayerAdded entirely and it will work.

Providing a purchase is completed, it will run that line of code.

Are you doing this in an actual test server, even a local one? Or are you just using solo test mode? Solo test mode does not act like an actual server.

Solo test, which is exactly where he will be testing his stuff - unless he wants to be charged for his own product every time.

image

Do you have this Studio setting enabled? There was another setting, but I can’t seem to find it, of which would make solo test mode act as a proper server. The script should also be in ServerScriptService.

But If I remove the PlayerAdded, then the player will be underlined. And If I put playerPurchasing, then it still doesn’t work. No clue why.

I do have it enabled, yes. However no solo test mode is going to make it act like an actual server, as servers are set up in a very different manner to a normal computer to optimise for efficiency.

You need to just fetch the player yourself, as opposed to having it built in.

This function will likely help in your sense:
https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayerByUserId

I’d give you a fully coded answer, however its not really within the rules to do that.

image

If you are experiencing that for whatever reason, this little section will allow you to run a local server and test things properly.

I’ve had this occur before in actual servers, where the code in the script usually is around 5000 lines long. The script is intensive but runs advanced mechanics in the game - the reality is all your local server is attempting to do is slow down my client enough to allow for the server to win, whereas this is not always the case especially when you have a thin client.

Again, I’ve never experienced this myself. As long as you have your events set before any other part of your code, then (from my experience), it will never have any issues. Running anything other than events will cause the script to start performing operations, which is the point in which a delay is created. If you place your events after executing code, that’s when events may miss a joining player.

But if you’re doing that, have your scripts in the right place and loaded on start, I guess keep doing what you’re doing. I’ve just never had this occur even in solo test mode.