How to Prompt The Player With The Closest Amount When They Are Missing Coins

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)
2 Likes

I’m a moderate scripter and don’t really know how table.sort and that sort of stuff works I just used this and edited it from another topic.

3 Likes

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.

Replace

With

local coinsProductIds = {
[250] = 1339932370,
[1000] = 1339932367,
[2500] = 1339932369,
[10000] = 1339932368
}

and

with

coinsProductIds[Values[i][1]]
1 Like

Thank you so much, that worked!

1 Like

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?

3 Likes

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

3 Likes

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
2 Likes

Nevermind I fixed it (chars chars)

1 Like

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

Is there anything I did wrong?

Remove or comment this, I’m doing all of this on mobile so I’m unable to test this all myself.

Also change

return a[2] < b[2]

to

return a < b

It still doesn’t work and only prompts the 250 coins one

I might just prompt it directly from where the player tried to buy the item and didnt have enough

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?

Correct (I need more character)

Try this;

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)

@TheRealBloxikGames So did it work? Let me know if it did.

Sure Im just testing something else out Ill give it ago

1 Like

What do I replace array with? I dont know.