Attempt to index nil with

  1. What do you want to achieve?
    If you buy the product you should get 1000 in-game currency.

  2. What is the issue?

player.CurrencyCash.Cash.Value = player.CurrencyCash.Cash.Value + 1000
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes, but I am new to scripting and I don’t understand the error mesage.

The Curency Script on the Server:

local currencyName = "Cash"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")

game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name = "CurrencyCash"
	folder.Parent = player

	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder
	
	local ID = currencyName.."-"..player.UserId
	local savedData = nil
	
	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)
	
	if savedData ~= nil then
		currency.Value = savedData
		print("Data Loaded")
	else
			-- new Player
			currency.Value = 10
			print("New player to the game")
	end
	
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.CurrencyCash.Cash.Value)
end)

game:BindToClose(function()
	
	-- When Game is ready to shut down
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game is shutting down")
		end
	end
	
	wait(5)
	
end)
-- This for testing with cubes

workspace:WaitForChild("Red").ClickDetector.MouseClick:Connect(function(player)
	player.CurrencyCash.Cash.Value = player.CurrencyCash.Cash.Value - 50
end)

workspace:WaitForChild("Green").ClickDetector.MouseClick:Connect(function(player)
	player.CurrencyCash.Cash.Value = player.CurrencyCash.Cash.Value + 5
end)

-- There is the problem

local MPS = game:GetService("MarketplaceService")

MPS.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == 996079489 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.ProductId)
		print("Player purchased")
		player.CurrencyCash.Cash.Value = player.CurrencyCash.Cash.Value + 1000
		print("Recieved gamepass")
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

The Local Button script:

MPS = game:GetService("MarketplaceService")
id = 996079489
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	MPS:PromptProductPurchase(player, id)
end)

Player purchased

10:28:18.516 - ServerScriptService.CurrencyScript:68: attempt to index nil with ‘CurrencyCash’

10:28:18.517 - Stack Begin

10:28:18.518 - Script ‘ServerScriptService.CurrencyScript’, Line 68

10:28:18.519 - Stack End

It works but the Problem is that you not get the Cash. Thank You for Helping

1 Like

Try doing this,
local currencyCash = player:WaitForChild("CurrencyCash")

currencyCash.Cash.Value = currencyCash.Cash.Value + 1000

Hope this helps.

2 Likes

You got the player from the product ID
Use receiptInfo.PlayerId instead

1 Like
attempt to index nil with ‘X’

This error means that wherever you’re trying to access the property/child X the thing you’re trying to access it on does not exist. In this case, you’re trying to access CurrencyCash on player, a nil value.

To fix this, you need to determine why player is nil. @Bindstep has explained why this is.

4 Likes

Now it says:

ServerScriptService.CurrencyScript:66: attempt to index nil with ‘WaitForChild’


local MPS = game:GetService("MarketplaceService")

MPS.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == 996079489 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.ProductId)
		local currencyCash = player:WaitForChild("CurrencyCash")
		print("Player purchased")
		currencyCash.Cash.Value = currencyCash.Cash.Value + 1000
		print("Recieved gamepass")
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

If I do something wrong, it makes me feel guilty, but I have very little experience.

Sorry, I’m also still new to scripting. It normally works for me when I use WaitForChild()

local MPS = game:GetService("MarketplaceService")

MPS.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == 996079489 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.ProductId) -- This should be PlayerId not ProductId
		print("Player purchased")
		player.CurrencyCash.Cash.Value = player.CurrencyCash.Cash.Value + 1000
		print("Recieved gamepass")
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

set UserId Instead Of ProductId :smiley:

1 Like

remember ProductId is not your UserId you would have to change it from ProductId to PlayerId

just as @BindStep said

1 Like

It worked. Thanks for helping. :grinning:

1 Like