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

Why is this script not giving me the gun and giving me an error on line 10?

Server Script

local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local gun = ReplicatedStorage:WaitForChild("Handgun")
local players = game:GetService("Players")
local receiptInfo = ga

local productId = 986593881

local function proccessReceipt(receiptInfo)
	
	local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		--the player probably left the game
		--if they come back, the callback will be recalled
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if player then
		-- what do you want the dev product to do
		local char = game.Workspace:FindFirstChild(player.Name)
		local gunClone = gun:Clone()
		gunClone.Name = "Gun"
		gunClone.Parent = char
	end
	
	return Enum.ProductPurchaseDecision.PurchaseGranted
	
end

--set callback
MarketplaceService.ProcessReceipt = proccessReceipt()

My local script

local MarketplaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")

local productId = 986593881
local player = game.Players.LocalPlayer

wait(3)

MarketplaceService:PromptProductPurchase(player, productId)

I am getting an error 16:19:56.739 - ServerScriptService.Script:10: attempt to index nil with 'PlayerId' and I am not getting the item, why?

1 answer

✓ Accepted answer

Problem:

  • You called the function while setting the callback. This would not work as the function would get called without a request to process. This would make it so that "receiptInfo" would be nil.

Solution:

  • Remove the "()" on line 31.

Explaining the Error:

The error: Attempt to index nil means you are trying to index something (properties, child, function, etc) that doesn't exist. In Lua (the coding language Roblox uses), things that don't exist are called nil.


Fixed ServerScript:

local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local gun = ReplicatedStorage:WaitForChild("Handgun")
local players = game:GetService("Players")
local receiptInfo = ga

local productId = 986593881

local function proccessReceipt(receiptInfo)

    local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
    if not player then
        --the player probably left the game
        --if they come back, the callback will be recalled
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end

    if player then
        -- what do you want the dev product to do
        local char = game.Workspace:FindFirstChild(player.Name)
        local gunClone = gun:Clone()
        gunClone.Name = "Gun"
        gunClone.Parent = char
    end

    return Enum.ProductPurchaseDecision.PurchaseGranted

end

--set callback
MarketplaceService.ProcessReceipt = proccessReceipt

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