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

How do I fix the: attempt to index nil with 'PlayerId' error?

Asked by 2 years ago

I have an error while trying to get the player/UserID when a dev product is purchased, here is my script:

local productID = 1270692852

local MPS = game:GetService('MarketplaceService')

local function processRecipet(RecieptInfo)

    local plr = game.Players:GetPlayerByUserId(RecieptInfo.PlayerId)

    if not plr  then
        -- plr probably left.
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end

    if RecieptInfo.PurchaseId == productID then
        if plr then
            local leaderstats = plr:WaitForChild('leaderstats')
            local Gems = leaderstats:WaitForChild('Gems')
            Gems.Value = Gems.Value + 50
        end
    end


    return Enum.ProductPurchaseDecision.PurchaseGranted

end

-- call back

MPS.ProcessReceipt = processRecipet()

The error is on this line:

    local plr = game.Players:GetPlayerByUserId(RecieptInfo.PlayerId)

All of this is in a server script.

0
The only problem now is that it isn't giving me the stuff, which I'll just make a new question for. starranger4 4 — 2y

2 answers

Log in to vote
0
Answered by
aviel101 165
2 years ago

that's because you're calling the function instead of passing the function address to MPS.ProcessReceipt. removing the brackets in line 29 should fix it

MPS.ProcessReceipt = processRecipet
Ad
Log in to vote
0
Answered by 2 years ago

I believe it has something to do with this.

MPS.ProcessReceipt = processRecipet()

try using this instead

MPS.ProcessReceipt:Connect(processRecipet)

Answer this question