I want to achieve a working dev product script that works.
What is the issue?
The issue is it doesn’t work with no errors it just doesn’t give the player the amount of currency.
What solutions have you tried so far?
Finding a solution.
local MarketplaceService = game:GetService("MarketplaceService")
function getPlayerFromId(id)
for i,v in pairs(game.Players:GetChildren()) do
if v.userId == id then
return v
end
end
return nil
end
MarketplaceService.ProcessReceipt = function(receiptInfo)
local productId = receiptInfo.ProductId
local playerId = receiptInfo.PlayerId
local player = getPlayerFromId(playerId)
if productId == 1232282811 then
player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 10000
elseif productId == 1232282877 then
player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 100000
elseif productId == 1232282925 then
player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1000000
elseif productId == 1232282987 then
player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 10000000
elseif productId == 1232283029 then
player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 100000000
elseif productId == 1232283060 then
player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1000000000
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
This may be due to the code running inside a LocalScript, multiple scripts listening for MarketplaceService.ProcessReceipt, and player, leaderstats, Strength being nil. I tried cleaning the code above in hopes of fixing the issue(if an instance is nil, the console will show a warning):
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
--[product id] = amount of strength
local products = {
[1232282811] = 10000,
[1232282925] = 1000000,
[1232282987] = 10000000,
[1232283029] = 100000000,
[1232283060] = 1000000000
}
MarketplaceService.ProcessReceipt = function(receiptInfo)
--pcall in case it fails
local Success, Error = pcall(function()
--there is a function for this
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
local leaderstats = player:FindFirstChild("leaderstats")
local Strength = leaderstats:FindFirstChild("Strength")
local amount = products[receiptInfo.ProductId]
Strength.Value += amount or 0 --if the product isn't part of the dictionary, we ignore it.
end)
if Success then
return Enum.ProductPurchaseDecision.PurchaseGranted
else
warn(Error)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
end
I tried this but then it doesn’t work! The first code you sent worked!
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
--[product id] = amount of strength
local products = {
[1232282811] = 10000,
[1232282877] = 100000,
[1232282925] = 1000000,
[1232282987] = 10000000,
[1232283029] = 100000000,
[1232283060] = 1000000000
}
--Gems--
local products2 = {
[1232275748] = 10000,
[1232275772] = 100000,
[1232275792] = 1000000,
[1232275869] = 10000000,
[1232275957] = 100000000,
[1232275978] = 1000000000
}
--Coins--
local products3 = {
[1232272876] = 10000,
[1232272728] = 100000,
[1232272939] = 1000000,
[1232273020] = 10000000,
[1232273262] = 100000000,
[1232273458] = 1000000000
}
MarketplaceService.ProcessReceipt = function(receiptInfo)
--pcall in case it fails
local Success, Error = pcall(function()
--there is a function for this
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
local leaderstats = player:FindFirstChild("leaderstats")
local Strength = leaderstats:FindFirstChild("Strength")
local Gems = leaderstats:FindFirstChild("Gems")
local Coins = leaderstats:FindFirstChild("Coins")
local amount = products[receiptInfo.ProductId]
Strength.Value += amount or 0 --if the product isn't part of the dictionary, we ignore it.
local amount2 = products2[receiptInfo.ProductId]
Gems.Value += amount or 0 --if the product isn't part of the dictionary, we ignore it.
local amount3 = products3[receiptInfo.ProductId]
Coins.Value += amount or 0 --if the product isn't part of the dictionary, we ignore it.
end)
if Success then
return Enum.ProductPurchaseDecision.PurchaseGranted
else
warn(Error)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
end
This is sort of not working, when the player buys the product it works. But if they leave and join back it goes back to 0! I already have a datastore that works, dw about that.