Money can't be given to player after purchase, "attempt to perform arithmetic"

Hello everyone!

I have a game where the player can purchase currency, but the issue is when they purchase I get the error below:

06:32:37.586  ServerScriptService.PurchaseProcessingManager:124: attempt to perform arithmetic (add) on number and nil  -  Server - PurchaseProcessingManager:124
  06:32:37.587  Stack Begin  -  Studio
  06:32:37.587  Script 'ServerScriptService.PurchaseProcessingManager', Line 124  -  Studio - PurchaseProcessingManager:124
  06:32:37.587  Stack End  -  Studio

Heres the PurchaseProcessingManager script, in ServerScriptStorage:

local marketService = game:GetService("MarketplaceService")
local dsService = game:GetService("DataStoreService")
local purchaseRecordsDataStore = dsService:GetDataStore("PurchaseRecordsDataStore")
local purchaseIds = {}

function getCoinAmount(productId)
	
	-- Return correct amount
	if productId == 104775869 then
		return 40000
	elseif productId == 104775889 then
		return 80000
	elseif productId == 104775928 then
		return 160000
	elseif productId == 104775989 then
		return 440000
	elseif productId == 104776022 then
		return 960000
	elseif productId == 104776060 then
		return 2080000
	end
	
end

function addToInvFolder(invFolder, item)
	
	-- Get folder
	local folder = invFolder:FindFirstChild(item.Parent.Name)
	
	-- Check if already there
	local exists = nil
	for _, i in pairs(folder:GetChildren()) do
		if i.Name == item.Name then
			exists = i
			break
		end
	end
	if exists == nil then
		
		-- Create new value
		local val = Instance.new("IntValue", folder)
		val.Name = item.Name
		val.Value = 1
		
	else
		
		-- Increment value
		exists.Value = exists.Value + 1
		
	end
	
end

marketService.ProcessReceipt = function(receiptInfo)
	
	-- Get information
	local plr = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
	local productId = receiptInfo.ProductId
	
	-- Get the player's purchase ids
	local plrIds = purchaseIds[tostring(plr.UserId)]
	
	-- Check if this is a duplicate attempt
	local okay = true
	for _, i in pairs(plrIds) do
		if i == receiptInfo.PurchaseId then
			print("blocked")
			okay = false
			break
		end
	end
	if not okay then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	-- Save attempt
	table.insert(plrIds, receiptInfo.PurchaseId)
	
		-- Get coin amounts
		local coinAmt = getCoinAmount(productId)
		local stats = plr.Stats:clone()
		
		-- Play effect
		game.ReplicatedStorage.Interactions.Client.BoughtCoinsEffect:FireClient(plr, coinAmt)
		
		-- Give the player his coins
		plr.Stats.Coins.Value = plr.Stats.Coins.Value + coinAmt
		stats.Coins.Value = stats.Coins.Value + coinAmt
		local saveResult = game.ServerScriptService.DataManager.SaveStats:Invoke(plr, stats)
		if saveResult == false then
			plr.Stats.Coins.Value = plr.Stats.Coins.Value - coinAmt
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
		
		-- Attempt to record the transaction (if it doesn't, oh well)
--		pcall(function()
--			purchaseRecordsDataStore:IncrementAsync(plr.UserId .. "_" .. productId, 1)
--		end)
		
		-- Print log
		print("LOG: " .. plr.Name .. " purchased product " .. productId .. " and received " .. coinAmt .. " coins!")
	
	-- Return a success
	return Enum.ProductPurchaseDecision.PurchaseGranted
	
end

Coins is an IntValue, is that an issue?

Im at a loss of how to fix it. When I revert to the very first version of the place, this issue isnt there. When i replace the whole purchasemanager script, the issue disappears. But when i bring it to the new place file and replace it, the issue persists. So theres something wrong with the current version?

I can provide more code samples if needed, if you know anything about this lmk :slight_smile:

maybe use tonumber() ? ??? ???

1 Like

You’re trying to add a number with nil (e.g. 5 + nil), that’s not possible.

It could be because getCounAmount() returned nil (Maybe the ProductId is not listed?)

wheres the 124 line?, just so i can review on it

	plr.Stats.Coins.Value = plr.Stats.Coins.Value + coinAmt

Ill check into that! I thought the ID’s were all correct

1 Like

did you try to add a tonumber to the number in the string?

I created all new ProductIDs and replaced them all in the script and the GUI and that looked to have fixed the issue!

1 Like

isaaa also a quick tip I learned a few days ago you can use plr.Stats.Coins.Value += coinAmt

same, you should really use it

Sick! ill look into implementing that :slight_smile: