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)
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
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).
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).
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.
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.
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.
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.