Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is my purchase system not working?

Asked by 3 years ago

The goal of this is to determine what product the person buys, and how many seconds it corresponds to in the table. Currently, it prints nil.

Script in ServerScriptService:

local PaymentTable = {
    ['1167769246'] = 1,
    ['1167769541'] = 55,
    ['1167769540'] = 10,
    ['1167769537'] = 30,
    ['1167769536'] = 60,
    ['1167769535'] = 300,
    ['1167769533'] = 600,
}

local MarketplaceService = game:GetService("MarketplaceService")

function processReceipt(receiptInfo)
    local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
    if not player then
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end
    -- script below controls what happens if bought 
    print(player.Name .. " just bought " .. receiptInfo.ProductId)
    --// do stuff

    --print(player.Name .. ' just bought ' .. PaymentTable[receiptInfo.ProductId])
    print(PaymentTable[receiptInfo.ProductId])

    return Enum.ProductPurchaseDecision.PurchaseGranted
end


MarketplaceService.ProcessReceipt = processReceipt

2 answers

Log in to vote
1
Answered by
174gb 290 Moderation Voter
3 years ago
Edited 3 years ago

That's because you inserted the numbers as string on PaymentTable, you can convert the Product Id (number) into string using tostring() example

local PaymentTable = {
    ['1167769246'] = 1,
    ['1167769541'] = 55,
    ['1167769540'] = 10,
    ['1167769537'] = 30,
    ['1167769536'] = 60,
    ['1167769535'] = 300,
    ['1167769533'] = 600,
}

local MarketplaceService = game:GetService("MarketplaceService")

function processReceipt(receiptInfo)
    local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
    if not player then
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end
    -- script below controls what happens if bought 
    print(player.Name .. " just bought " .. receiptInfo.ProductId)
    --// do stuff
    local stringId = tostring(receiptInfo.ProductId)
    --print(player.Name .. ' just bought ' .. PaymentTable[receiptInfo.ProductId])
    print(PaymentTable[stringId])

    return Enum.ProductPurchaseDecision.PurchaseGranted
end


MarketplaceService.ProcessReceipt = processReceipt
Ad
Log in to vote
1
Answered by 3 years ago

Try using tostring on the receipt's ProductId; this returns a conversion to string. But as a better practice, I would recommend using the actual valuetype the ProductId is; as it helps you understand what is and isn't accepted as an index.

The script is slightly slower using tostring, so I would recommend removing the quotations from the numbers in your table, rather than using tostring. Although not very noticeable of a difference, it helps to understand that performance is important.

Answer this question