I have this function that is connected to a remote event which all seems to work except that it is erroring: “ServerScriptService.Donate script:18: attempt to index nil with ‘PlayerId’” when the game is started.
local function processReceipt(receiptInfo)
local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
if player then
if receiptInfo.ProductId == devItemID_N1 then
print("Succesfully bought 25R$")
return Enum.ProductPurchaseDecision.PurchaseGranted
elseif receiptInfo.ProductId == devItemID_N2 then
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
end
marketplace.ProcessReceipt = processReceipt
remoteEvent.OnServerEvent:Connect(processReceipt())
For server scripts that are connected to a RemoteFunction, the first argument must be player. For example make the function parameter say function(player)
You don’t need to bind the ProcessReceipt to a OnServerEvent event. Remove the following line and remote event associated with it then try again, calling marketplace:PromptProductPurchase() where the player donates should be enough.
I put all the code together and it is still erroring (it is in a local script)
-- Create Vars --
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local button = script.Parent
local marketplace = game:GetService("MarketplaceService")
local localPlayer = game:GetService("Players").LocalPlayer
local devItemID_N1 = 1655061162
local devItemID_N2 = 1656666021
-- Create Function --
local function processReceipt(receiptInfo)
if not localPlayer then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
if localPlayer then
if receiptInfo.ProductId == devItemID_N1 then
print("Succesfully bought 25R$")
return Enum.ProductPurchaseDecision.PurchaseGranted
elseif receiptInfo.ProductId == devItemID_N2 then
print("Succesfully bought 50R$")
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
end
-- Connect mouse button click to function --
button.MouseButton1Click:Connect(function()
print("Buying ".. button.Name)
marketplace:PromptProductPurchase(localPlayer, devItemID_N1)
processReceipt()
end)
So in the original post you said this was a server script in server script service, but I guess you’ve moved it to a local script because it looks like the same code reformatted for a local script, but a few things aren’t exactly clear
Firstly I’ll assume that you moved the script to a local one. If this is the case, why?
Secondly and most importantly, what is the error?
Thirdly, if you’re using both a server and local script, it would be helpful to post both each time your edit the scripts so we have the full picture
If you provide some more information I’m sure some experienced individual will be able to solve this
-- Create Vars --
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local button = script.Parent
local remoteEvent = game:GetService("ReplicatedStorage"):FindFirstChild("DonateEvent")
local marketplace = game:GetService("MarketplaceService")
local localPlayer = game:GetService("Players").LocalPlayer
local devItemID_N1 = 1655061162
local devItemID_N2 = 1656666021
-- Connect mouse button click to function --
button.MouseButton1Click:Connect(function()
print("Buying ".. button.Name)
marketplace:PromptProductPurchase(localPlayer, devItemID_N1)
local reciptInfo = marketplace.ProcessReceipt()
remoteEvent:FireServer(localPlayer, reciptInfo)
end)
Server script:
-- Create Vars --
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local marketplace = game:GetService("MarketplaceService")
local remoteEvent = game:GetService("ReplicatedStorage"):FindFirstChild("DonateEvent")
local players = game:GetService("Players")
local devItemID_N1 = 1655061162
local devItemID_N2 = 1656666021
-- Create Function --
local function processReceipt(player, receiptInfo)
local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return nil
end
if player then
if receiptInfo.ProductId == devItemID_N1 then
print("Succesfully bought 25R$")
return Enum.ProductPurchaseDecision.PurchaseGranted
elseif receiptInfo.ProductId == devItemID_N2 then
print("Succesfully bought 50R$")
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
end
marketplace.ProcessReceipt = processReceipt
remoteEvent.OnServerEvent:Connect(function(player, reciptInfo)
marketplace.ProcessReceipt(reciptInfo)
end)
Error:
“18:07:30.904 ProcessReceipt is a callback member of MarketplaceService; you can only set the callback value, get is not available - Client - LocalScript:18”
Edit: It now works:
REMOVE “local reciptInfo = marketplace.ProcessReceipt()
remoteEvent:FireServer(localPlayer, reciptInfo)”
REMOVE “remoteEvent.OnServerEvent:Connect(function(player, reciptInfo)
marketplace.ProcessReceipt(reciptInfo)
end)”
To be honest, I don’t have much experience with this particular sort of thing, but after checking the documentation on the MarketplaceService, I did notice the error in your use of ProcessReceipt. What were you trying to achieve with it though?