Why is my purchase system not working?

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 at this part:

print(PaymentTable[receiptInfo.ProductId])

I’ve tried to debug it with various methods, and just can’t find the problem.

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

Try setting receiptInfo.ProductId into a string using tostring(). For example,

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

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

    return Enum.ProductPurchaseDecision.PurchaseGranted

My guess is that since ProductId is a number, it is looking for the value in that position instead of accessing the actual key.

1 Like