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 