Help accessing player ID in server script (thanks!)

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())

Thanks for reading!

4 Likes

The error means that receiptInfo is nil, try to print it out to see if it is

This will give an error, change that to this instead:

remoteEvent.OnServerEvent:Connect(processReceipt)

By the way can you show us the local script firing this remote event?

3 Likes

For server scripts that are connected to a RemoteFunction, the first argument must be player. For example make the function parameter say function(player)

2 Likes

it is now saying PlayerId is not a valid member of Player “Players.EffBeeCee”
edit: sorry for late response

1 Like

So it’s actually UserID not PlayerID—basically Player dot would be followed by UserID

Player.UserID

1 Like

Also, please post the server script as you make changes
(And the local script if you’re comfortable with that)

1 Like

Change this:

To:

remoteEvent.OnServerEvent:Connect(function(plr, receiptInfo)
	processReceipt(receiptInfo)
end)
1 Like

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. :slight_smile:

remoteEvent.OnServerEvent:Connect(processReceipt())
1 Like

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)
1 Like

What does it error also you aren’t supposed to manually process receipt, you do it on the server…

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

BOTTOM FOR SOLUTION

local script:

-- 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?

What ended up fixing it? @EffBeeCee

Check bottom of post, I didn’t realize that the callback function was triggered automatically.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.