Error Message - Argument 2 missing or nil (How do I fix this?

I’m trying to have a donation board in the new game I am making. I am not a scripter so I know very little to nothing about scripting, I had to watch a youtube tutorial for this script. If anyone can help me fix this error that would be awesome! The buttons on my donation board are also not working but I am guessing it has something to do with the error message


Output messages:


Script:

local donateAmounts = {"10", "50", "100", "1000", "5000", "50000", "100000"}
local ids = {1252536320, 1252544596, 1252544641, 1252544712, 1252544777, 1252544840, 1252544903}


local mps = game:GetService("MarketplaceService")
local dss = game:GetService("DataStoreService")
local ods = dss:GetOrderedDataStore("Donators")


for i, amount in pairs(donateAmounts) do
	
	local donateButton = script:WaitForChild("DonateButton"):Clone()
	donateButton.Text = amount .. " Robux"
	donateButton.Parent = script.Parent.DonatePart.DonateGui.DonateList
end


game.ReplicatedStorage.DonateRE.OnServerEvent:Connect(function(plr, button)
	
	local amount = string.gsub(button.Text, " Robux", "")
	local id = ids[table.find(donateAmounts, tonumber(amount))]
	
	mps:PromptProductPurchase(plr, id)
end)


mps.ProcessReceipt = function(purchaseInfo)
	
	local amount = mps:GetProductInfo(purchaseInfo.ProductId, Enum.InfoType.Product).PriceInRobux
	
	local success, err = pcall(function()
		local totalDonated = ods:GetAsync(purchaseInfo.PlayerId) or 0
		ods:SetAsync(purchaseInfo.PlayerId, totalDonated + amount)
	end)
	
	return success and Enum.ProductPurchaseDecision.PurchaseGranted or Enum.ProductPurchaseDecision.NotProcessedYet
end


while wait(5) do
	
	for i, child in pairs(script.Parent.LeaderboardPart.LeaderboardGui.LeaderboardList:GetChildren()) do
		
		if child:IsA("Frame") then child:Destroy() end
	end
	
	
	local pages = ods:GetSortedAsync(false, 100)
	local top = pages:GetCurrentPage()
	
	
	for rank, data in ipairs(top) do
		
		local username = game.Players:GetNameFromUserIdAsync(data.key)
		local donated = data.value
		
		local leaderboardFrame = script.LeaderboardFrame:Clone()
		leaderboardFrame.Rank.Text = "#" .. rank
		leaderboardFrame.Username.Text = username
		leaderboardFrame.Amount.Text = donated
		
		leaderboardFrame.Parent = script.Parent.LeaderboardPart.LeaderboardGui.LeaderboardList
	end
end

The error is on line 23 of your script:

local id = ids[table.find(donateAmounts, tonumber(amount))]

An argument is what you give to a function (like entering what item you want on a vending machine). Your function table.find receives up to 3 arguments. Here, the third argument will be irrelevant.

Argument 1 (of table.find) is what array/table you are searching in (what vending machine are you using?), the error message does not mention this argument, so we ignore this argument.

Argument 2 is what you are searching for (what is the item associated with the code you inputted in the vending machine?). You put in tonumber(amount) which means you are converting something to number form. The error mentions argument 2, so this is the source of your error because you have not determined if it did convert into a number.

Lua’s tostring converts any string that aren’t only numbers (“Gi2” and “e” are examples) into nil. Your error message says the second argument is nil.

You can fix your line of code in any way you want, if you’re only looking for the numbers in your string (button.Text is the string), you can replace line 22 with:

local amount, _ = string.gsub(button.Text, "%D", "")

(%D searches for all non-digit characters and the gsub replaces those with empty space, reference here)


This explanation was made for a beginner coder in mind. I hope this helps.

Thank you for the response, I’ve tried replacing line 22 with what you listed and it did the same error.

In the video that I’ve watched everything worked so, I am not sure why mine isn’t working as it’s a direct copy of the person that I am following the tutorial of.

I fixed the error message and the button not prompting purchases, I did this by removing the quotation marks on all the numbers. The only issue now is that I don’t pop up on the donation leaderboard