Attempt to index nil with 'FindFirstChild'

So, I got an error on my script for developer products. I can’t manage to fix it, here’s the script:

local MarketplaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")
local SG = game:GetService("StarterGui")
local productID = 1347531300

local function processReceipt(receiptInfo)
	
	local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if player then
		local cash = game.Players.LocalPlayer:FindFirstChild("leaderstats"):FindFirstChild("Cash")
		cash += SG.Utilities.UIs.ShopMain.InFrame.ButtonsFrame.Cash.Shadow1.BuyButton1.InfoLabel.ChangeGiveCash.GiveCash.Value
	end
	
end

MarketplaceService.ProcessReceipt = processReceipt

Here’s the error:

1 Like

its not finding the player from what i could see but im probably wrong
maybe try if player ~= nil ?

1 Like

Attempt to index nil means you are trying to access a property of nil, aka one of the FindFirstChild’s is returning nil, or game.Players.LocalPlayer is nil. There is no point in using FindFirstChild if you have no regard for what it returns.

also are you trying to process receipts on the client???

1 Like

I fixed it with something like that:

local players = game:GetService("Players")
local SG = game:GetService("StarterGui")
local productID = 1347531300

local function processReceipt(receiptInfo)
	
	local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if player then
		game.Players.PlayerAdded:Connect(function(player)
			local cash = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Cash")
			cash.Value += SG.Utilities.UIs.ShopMain.InFrame.ButtonsFrame.Cash.Shadow1.BuyButton1.InfoLabel.ChangeGiveCash.GiveCash.Value
		end)
	end
	
end

MarketplaceService.ProcessReceipt = processReceipt

My goal for this script is to make a developer product for a simulator that is going to give me a certain amount of cash that is set in a NumberValue.
I have no errors in the output but this doesn’t seem to work.

1 Like

Is this in a server script or local script? You can’t use LocalPlayer in server scripts.

2 Likes

Oh. But I have to keep it as server script for it to work…

1 Like

Instead of doing

	local cash = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Cash")

Do

	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
1 Like

Ideal code would look something like this:

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local StarerGui = game:GetService("StarterGui")

local productID = 1347531300

local function ProcessReceipt(ReceiptInfo)
	local Player = Players:GetPlayerByUserId(ReceiptInfo.PlayerId)
	if not Player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	Player.leaderstats.Cash.Value += StarerGui.Utilities.UIs.ShopMain.InFrame.ButtonsFrame.Cash.Shadow1.BuyButton1.InfoLabel.ChangeGiveCash.GiveCash.Value
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketplaceService.ProcessReceipt = ProcessReceipt

I wouldn’t recommend having having a wide list of indexing though such as StarerGui.Utilities.UIs.ShopMain.InFrame.ButtonsFrame.Cash.Shadow1.BuyButton1.InfoLabel.ChangeGiveCash.GiveCash.Value
it damages the codes readability

1 Like

I changed to this line but it didn’t work… I don’t have any errors tho

1 Like

It makes the script shorter and easier to read but I don’t know why, I don’t recieve anything

1 Like

On line 14, replace the game.Players.LocalPlayer with game.Players:GetPlayerByUserId(receiptInfo.PlayerId). I’m assuming this script is a server script since it is located in the ServerScriptStorage, and accessing local players from there usually cause problems.

1 Like
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local StarerGui = game:GetService("StarterGui")

local productID = 1347531300

local function ProcessReceipt(ReceiptInfo)
	print("Proccessing reciept")
	
	local Player = Players:GetPlayerByUserId(ReceiptInfo.PlayerId)
	if not Player then
		warn("Player is not ingame, not yet proccessed payment")
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	print(string.format("%s has %i cash, incrimenting by %i", Player.Name, Player.leaderstats.Cash.Value, StarerGui.Utilities.UIs.ShopMain.InFrame.ButtonsFrame.Cash.Shadow1.BuyButton1.InfoLabel.ChangeGiveCash.GiveCash.Value))
	
	Player.leaderstats.Cash.Value += StarerGui.Utilities.UIs.ShopMain.InFrame.ButtonsFrame.Cash.Shadow1.BuyButton1.InfoLabel.ChangeGiveCash.GiveCash.Value
	print("Purchase granted")
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketplaceService.ProcessReceipt = ProcessReceipt

Try this and look at your output and tell me what it spat out.

1 Like

I got this
image

1 Like

Well then, clearly the reason why your cash is not changing is because
StarerGui.Utilities.UIs.ShopMain.InFrame.ButtonsFrame.Cash.Shadow1.BuyButton1.InfoLabel.ChangeGiveCash.GiveCash.Value equals 0

2 Likes

But it has a value…
image
How is that possible?

1 Like

It’s not equal to 0, but, when I changed the script with something else like
Player.leaderstats.Cash.Value += Player.leaderstats.Cash.Value
then it worked.
So i think the problem is that the game doesn’t read a number value in the Starter GUI?

1 Like

Don’t change values in StarterGui. They’re replicated to the player when they join.

3 Likes

Okay so I changed my script and put it in Workspace but now I have the error:
image

Here’s the script:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function()	
	while wait(.5) do
		local formula = ((game.Players.LocalPlayer.leaderstats.Multiplier.Value/1.5)*game.Workspace.ServerMulti.OldValue.Value*5)/10  -- Multi.Value * ServerMultiplier * EventMultiplier/10
		if game.Workspace["Usefull Informations"].PlayerInformations.VIP.Value == true then
			formula = formula*5
		end
		script.GiveCash.Value = formula*100*(3600/2)
	end
end)
1 Like

LocalPlayer only exists within LocalScripts. Also, don’t put these kinds of scripts in the Workspace.

Also, what’s the loop for? I’m very sure you don’t need it.

1 Like

Is the Folder’s name leaderstats?

If no, add it.
If yes, it might be a bug. Or change it to :WaitForChild("leaderstats").

1 Like