Hello every Developpers !
Today i add to my game a coins shop (coins to purchase objects). But when i want to buy it,
it does not give the amount entered… Please help me
Ok cool, where is the code So we can help?
This is the LocalScript for 50 coins :
local Event = game.ReplicatedStorage.BUYCoins
local Button = script.Parent
local CoinsValue = 50
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local productId = 1503289755
Button.MouseButton1Click:Connect(function()
MarketplaceService:PromptProductPurchase(player, productId)
end)
This is the Script for the 50 coins in ServerScriptService :
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ProductID = 1503289755
local function processReceipt(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
if player then
player.leaderstats.Coins.Value += 50
end
end
MarketplaceService.ProcessReceipt = processReceipt
thank you for your very quick response
Because you arent telling the code to give you the amount for the Product because you are not checking if the ProductId
matches.
check using Receipt.ProductId
to make sure its the Correct Id, like this:
if receiptInfo.ProductId == ProductId then
player.leaderstats.Coins.Value += 50
end
However, if you want to speed up the Process and not add so many if
statements to check each one, you can have a table do it for you, like so:
local Products = {
[ProductId] = Amount
-- Insert the Id into the Brackets, and the value within the 'Amount' part.
-- here you are basically creating an index, where the ProductId can be access by its Id and when you index it, it would give you its value
}
local function processReceipt(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
for ProductId, Amount in Products do -- looks through the entire table
if receiptInfo.ProductId = ProductId then -- if one of the Ids inside the table matches
player.leaderstats.Coins.Value += Amount -- adds the Amount the Id was holding in the Table
end
end
end
MarketplaceService.ProcessReceipt = processReceipt
The Other thing is that you arent completing the purchase, without telling the code that the purchase was completed, it will double the amount you were trying to get from the purchase as the code basically think thatvthe code code hasnt ended, which isnt something you want, so finish the callback with return Enum.ProductPurchaseDecision.PurchaseGranted
to tell the code that the Purchase was a Success.
-- rest of the callback
return Enum.ProductPurchaseDecision.PurchaseGranted -- Tell the game that the Purchase is Complete
end
MarketplaceService.ProcessReceipt = processReceipt
Don’t forget to add commas within the table, like this:
t = {
key = value, -- separate the index with a comma which is this key: ,
}
ex = {1, 2, 3, 4, 5} -- a comma is separating each value.
Did i do correctly ? :
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Products = {
[1503289755] = 50,
[1503300965] = 100,
[1503301752] = 200,
[1503303427] = 500,
[1503303558] = 1000
-- Insert the Id into the Brackets, and the value within the 'Amount' part.
-- here you are basically creating an index, where the ProductId can be access by its Id and when you index it, it would give you its value
}
local function processReceipt(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
for ProductId, Amount in Products do -- looks through the entire table
if receiptInfo.ProductId == ProductId then -- if one of the Ids inside the table matches
player.leaderstats.Coins.Value += Amount -- adds the Amount the Id was holding in the Table
end
end
-- rest of the callback
return Enum.ProductPurchaseDecision.PurchaseGranted -- Tell the game that the Purchase is Complete
end
MarketplaceService.ProcessReceipt = processReceipt
yes.
IT WORK THANK YOU !!! I was deleted it when i don’t find solutions
if it works, mark the post as the Solution.
@Hlruko_o
Also keep in mind, this puts all of your Scripts for each Product into 1 Single script, so you can put this inside ServerScriptService
and it will still work as normal, so you can basically remove the others.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.