code. the code does not work. i tried troubleshooting with output + printing but now theres no errors and it doesnt work as intended. it doesnt add the coins to your balance and ive checked the paths many times, but it still does not work.
if someone understands my issue please let me know asap i need sleep
If you want to learn how to debug you need to learn how to use print statements. Print out the player’s name, print out something simple to see if each part of the script works. I believe one problem is that you are doing
local player = game.Players:GetPlayerByUserId(receiptInfo.UserId)
when you should be doing
local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
Also please copy and paste your code rather than screenshotting it is just annoying to write out parts of people’s code.
Instead of receiptInfo.UserId you should use receiptInfo.PlayerId, I tried cleaning your code to avoid bad scripting habits(like repetitive if statements, WaitForChild inside a yielding function, not using a pcall to avoid errors, handling situations etc.)
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
--format [coin amount] = product id
local products = {
[100] = 1229114461,
[1000] = 1229114462,
[5000] = 1229114463
}
MarketplaceService.ProcessReceipt = function(receiptInfo)
local Player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not Player then
--if the player is not found, we ignore the request
return Enum.ProductPurchaseDecision.NotProcessedYet
end
--we loop through each product
for amount, product in pairs(products) do
--we keep skipping products until we find the one matching
if receiptInfo.ProductId ~= product then continue end
--if the product matches the we wrap the coin change in a pcall to know if it fails
local Success, Error = pcall(function()
Player.leaderstats.Coins.Value += amount
end)
--if the coins do chance, then actually pay for the product
if Success then
return Enum.ProductPurchaseDecision.PurchaseGranted
else
warn(Error)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
end
--in case the product id isn't found(the purchase is invalid) it just fails
--you can remove the line below if you just want to ignore invalid products
return Enum.ProductPurchaseDecision.NotProcessedYet
end
local mps = game:GetService("MarketplaceService")
local coins100 = 1229114461
local coins1000 = 1229114462
local coins5000 = 1229114463
local function processReceipt(receiptInfo)
local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
if player then
if receiptInfo.ProductId == coins100 then
local ls = player:WaitForChild("leaderstats")
ls.Coins.Value = ls.Coins.Value + 100
end
if receiptInfo.ProductId == coins1000 then
local ls = player:WaitForChild("leaderstats")
ls.Coins.Value = ls.Coins.Value + 1000
end
if receiptInfo.ProductId == coins5000 then
local ls = player:WaitForChild("leaderstats")
ls.Coins.Value = ls.Coins.Value + 5000
end
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
mps.ProcessReceipt = processReceipt
The script works perfectly fine when I play-test it, add a print("success") inside the pcall to see if the script is running at intended(also add another print on the top of the function to see if the event fires at all).
local mps = game:GetService("MarketplaceService")
local coins100 = 1229114461
local coins1000 = 1229114462
local coins5000 = 1229114463
local function processReceipt(receiptInfo)
local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
if player then
local ls = player:WaitForChild("leaderstats")
local coins = ls:WaitForChild("Coins")
if receiptInfo.ProductId == coins100 then
coins.Value += 100
elseif receiptInfo.ProductId == coins1000 then
coins.Value += 1000
elseif receiptInfo.ProductId == coins5000 then
coins.Value += 5000
end
return Enum.ProductPurchaseDecision.PurchaseGranted
else
return Enum.ProductPurchaseDecision.NotProcessedYet
end
end
mps.ProcessReceipt = processReceipt