Attempt to perform arithmetic (mul) on nil and number

I’m making a system that prompts the player to the closest amount of purchasable coins in a dev product and I’m sending a number through 2 evens.

My issue is, the number I’m sending is negative and when I multiply it by -1, I expect it to be positive, that’s simple math. Instead, I get an error: "attempt to perform arithmetic (mul) on nil and number "

First send:

game.ReplicatedStorage.ErrorEvents.NotEnoughCoins:FireServer(game.Players.LocalPlayer.leaderstats.Coins.Value + 200 * -1)

Second send:

game.ReplicatedStorage.ErrorEvents.NotEnoughCoins.OnServerEvent:Connect(function(Player, Coins)
	game.ReplicatedStorage.ErrorEvents.NotEnoughCoins:FireClient(Player, Coins)
end)

Getting the number and multiplying it by -1 to make it positive

if (MissingCoins * -1) <= 250 --etc.

I’ve printed the number in many places and it always prints as a negative number and not nil. I don’t understand why this is happening.

3 Likes

I do have a question, why make the number negative when firing the first event and converting it back at the end? Also by a first glance there shouldn’t be any issue with the code

2 Likes

I can’t figure out how to get the missing coins otherwise. In the system is takes the player’s coins and substracts the item’s cost from it and I have to multiply it by -1 before the subtraction for it to work, otherwise for example if coins = 97 and cost = 200, with the *-1 it would be -103, and then i’d just have to make it positive, but without *-1, it would be 297 for some reason.

2 Likes

Have you figured anything out?

1 Like

Well that means one of the numbers being multiplied is nil. Im not sure out of the 3 code examples youve show ln which specifically is causing the error, but its most likely the 3rd one, because if it was the first one the error would be different, and it cant be the second because theres no equations. Try printing out the MissingCoins variable before using it in the equation.

1 Like

It print’s two numbers, strange. First nil, then -103 because the player had 97 and the item costs 200.

1 Like

What do you think is wrong, is it just a roblox error?

1 Like

its not a roblox error, have you tried using math,abs() instead of (MissingCoins * -1)?

I don’t know how that works, do i put “lua”, Im guessing not, and would I add MissingCoins inside the bracket of math.abs?

no sorry that was a typo
image
this will convert any negative number to a positive number.

so

math.abs(MissingCoins)
```?
1 Like

yeah go ahead and try that ( i couldnt just type in “yes”) :sob:

I know character limit is so annoying

Lol I got this error: invalid argument #1 to ‘abs’ (number expected, got nil)

if math.abs doesnt work, we might need more information on “missingCoins” because thats the main problem since its coming back as nil.

okay yeah so missing coins is the problem. can you show us more information on how you get that variable

It start off as Coins - 200 * - 1, then it’s sent through an event and sent back so it shouldb e the same number

game.ReplicatedStorage.ErrorEvents.NotEnoughCoins:FireServer(game.Players.LocalPlayer.leaderstats.Coins.Value + 200 * -1)

then

game.ReplicatedStorage.ErrorEvents.NotEnoughCoins.OnServerEvent:Connect(function(Player, Coins)
	game.ReplicatedStorage.ErrorEvents.NotEnoughCoins:FireClient(Player, Coins)
end)

and then heres where math.abs didn’t work

game.ReplicatedStorage.ErrorEvents.NotEnoughCoins.OnClientEvent:Connect(function(MissingCoins)
	local Coins250 = 1339932370
	local Coins1000 = 1339932367
	local Coins2500 = 1339932369
	local Coins10000 = 1339932368
	
	local Values = {{250,Coins250}, {1000,Coins1000}, {2500,Coins2500}, {10000,Coins10000}}
	
	local ClosestTo = math.abs(MissingCoins)
	local FetchCount = 1
	local n=#Values
	for i = 1,n  do
		if ClosestTo<Values[1][1] then
			local abc={Values[1][1], math.abs(ClosestTo - Values[1][1]),Values[1][2]}
			table.insert(Values,abc)
		end
		table.remove(Values,1)
	end

	table.sort(
		Values,
		function(a, b)
			return a[2] < b[2]
		end
	)

	for i = 1, FetchCount do
		game:GetService("MarketplaceService"):PromptProductPurchase(game.Players.LocalPlayer, Values[i][3])
	end
end)

I might just do this instead for the first line

game.ReplicatedStorage.ErrorEvents.NotEnoughCoins:FireServer(math.abs(game.Players.LocalPlayer.leaderstats.Coins.Value + 200 * -1))

Now i"m getting "attempt to compare nil < number " in the last script