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 "
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
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.
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.
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)