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

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

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.

1 comment

0

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

2 answers

✓ Accepted answer

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

I believe it has something to do with this.

MPS.ProcessReceipt = processRecipet()

try using this instead

MPS.ProcessReceipt:Connect(processRecipet)

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