Script errors when trying to get the length of my number

I am currently writing a simple system that adds a K, M or B in front of numbers. This needs to match the roblox leaderboard which only goes up to B. I do this by checking the number’s length. The only issue is that it gives me the error “attempt to get length of a number value”.

local Price = script.Parent.Price.Value

function roundNumber(num, numDecimalPlaces)
	return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end

if (#Price >= 4 or #Price <= 6) then
	Button.UI.Price.Text = tostring(roundNumber(Price/1000, 2)) .."K$"
elseif (#Price >= 7 or #Price <= 9) then
	Button.UI.Price.Text = tostring(roundNumber(Price/1000000, 2)) .."M$"
elseif (#Price >= 10) then
	Button.UI.Price.Text = tostring(roundNumber(Price/1000000000, 2)) .."B$"
end

if im right using “#” to get the length of something only works on strings and tables, convert the number to a string using tostring() then check the length of it.

example:

local Num = 3683

print(#tostring(Num)) -->> 4

if you need the number to still be a number after checking the length then you can use tonumber() to convert it back into a number

Here is a simple script to do what you want, try running the script, but note that I am unable to test it as I don’t have access to your work. Anyways, I want to remind you that I didn’t write this script in a studio environment, so there’s a possibility that it may not work as intended. Let me know if it’s not worked as intented. If i fixed the issue please make sure to close your support topic by check the solution.

You can also like to support :123: :slightly_smiling_face:

Script

local Price = script.Parent.Price.Value

function roundNumber(num, numDecimalPlaces)
    return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end

local priceString = tostring(Price)

if (#priceString >= 4 and #priceString <= 6) then
    Button.UI.Price.Text = tostring(roundNumber(Price / 1000, 2)) .. "K$"
elseif (#priceString >= 7 and #priceString <= 9) then
    Button.UI.Price.Text = tostring(roundNumber(Price / 1000000, 2)) .. "M$"
elseif (#priceString >= 10) then
    Button.UI.Price.Text = tostring(roundNumber(Price / 1000000000, 2)) .. "B$"
end

Explanation
The # operator is used to get the length of a string, not a number.

Test place
//

~ @hollaquetalBRUH

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.