Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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.

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

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

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.