I’m trying to make a system where if the player has not enough coins then they get prompted with the closest amount of coins (devproduct) to their coins.

What did I do wrong? I’m getting “Unable to cast string to int64”

game.ReplicatedStorage.ErrorEvents.NotEnoughCoins.OnClientEvent:Connect(function()
local Coins250 = 1339932370
local Coins1000 = 1339932367
local Coins2500 = 1339932369
local Coins10000 = 1339932368
local Values = {250, 1000, 2500, 10000}
local ClosestTo = game.Players.LocalPlayer.leaderstats.Coins.Value
local FetchCount = 1
for i = 1, #Values do
Values[i] = {Values[i], math.abs(ClosestTo - Values[i])}
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, "Coins"..Values[i][1])
end
end)

This code doesn’t reference one of the ID product variables and instead is creating a string value which is “Coins#”, causing the cast string to Int64 error.
To fix this you should instead store the coins products in a table with the keys as the amount of coins and the values as the product IDs.

Also if I add the coins the player needs to buy the item then there’s a chance it would go to the nearest product that’s slightly lower, like if it’s 300 coins and it prompts them with 250 coins purchase, how do I fix that?

For example if game.Replicated.ErrorEvents.NotEnoughtCoins.OnClientEvents:Connect(function(MissingCoins)) how do I make that selected product bigger than missing coins?

Well I’m assuming that instead of the closest one, you’d want the smallest one that is enough for them to purchase what they’re trying to buy. Using the values table you can use a for loop to go through the values from smallest to largest and then once you find one which is enough you can prompt the pass and break the loop

for i,coinValue in pairs(values) do
if MissingCoins <= coinValue or i == #values then
game:GetService("MarketplaceService"):PromptProductPurchase(game.Players.LocalPlayer, coinProductIds[coinValue])
break
end
end

This is the script and it’s giving two errors each time:

game.ReplicatedStorage.ErrorEvents.NotEnoughCoins.OnClientEvent:Connect(function(MissingCoins)
local CoinsProductIds = {[250] = 1339932370, [1000] = 1339932367, [2500] = 1339932369, [10000] = 1339932368}
local Values = {250, 1000, 2500, 10000}
local ClosestTo = game.Players.LocalPlayer.leaderstats.Coins.Value
local FetchCount = 1
for i = 1, #Values do
Values[i] = {Values[i], math.abs(ClosestTo - Values[i])}
end
table.sort(
Values,
function(a, b)
return a[2] < b[2]
end
)
for i, coinValue in pairs(Values) do
if MissingCoins <= coinValue or i == #Values then
game:GetService("MarketplaceService"):PromptProductPurchase(game.Players.LocalPlayer, CoinsProductIds[coinValue])
break
end
end
end)

attempt to compare number <= table, attempt to compare nil <= table

So what you’re trying to do is prompt the player with a purchase for coins but you want to prompt them with the closest amount of coins so that they can buy the intended item?

local closest = math.huge
local number = (put a number here)
for _, otherNumber in pairs(array) do
if math.abs(number - otherNumber) < math.abs(number - closest) then
closest = otherNumber
end
end
print(number)