How can I limit a string to only 3 or below characters

I am trying to make a how many studs away from part script and I don’t want the extra parts of the distance. (e.g / 592.23482747128412) Is there anyway to remove the numbers after the .

My script uses DistanceFromCharacter.

You can use string.sub for this:

local var = "12345" -- Your string
print(string.sub(var, 1, var.len(var) - 3))

Oops, use var.len(var) - 2, not 3, I got it wrong.

Wait, I forgot to read the whole topic…
Use string.split(var, ".")[1]

you would want it like this:

local str = "592.23482747128412"
str = #str.split(".") > 1 and str.split(".")[1] or str

@BriefYayyayyay beat me to it, but theirs would work.

As I said I am using DistanceFromCharacter.

             Studs.Text = ""..Plr:DistanceFromCharacter(HRP).." studs"
             string.sub(Studs.Text,1,Studs.Text.len(Studs.Text)- 3) 

I am real confused on how this will work.

Note: I need it to auto update that why theres a renderstepped

string.sub(yourString , 1 , 3)

It would be partially the way I showed it.

    local dist = Plr:GetDistanceFromCharacter(HRP)
    dist = tostring(dist).split(".")[1]
    Studs.Text = ""..dist.." studs"

Well it works but it just shows the . not anything else lol

but it shouldn’t show the dot? It quite literally splits at the dot so the dot shouldn’t even be seeable.

the output is . studs away literally only the dot lol

You can do this like so:

local chars = str:sub(1, math.min(str:len(), 3))

This should give a max of 3 characters I believe, and str is your string.

I’ll test some stuff to try and figure out the problem

Use the math.floor function. If you specifically need it to be a string you can add .."" after it.

Have you tried math.floor(number)?

This will round down but if you want to round up just do math.floor(number + .5)

Bro… you want to remove the decimals?

number = math.floor(number)

or you can round

function roundNumber(num, numPlaces)
	return math.floor(num*(10^numPlaces))/(10^numPlaces)

-- 199 is the number
print(roundNumber(NUMBER_HERE, 2))

or u can manually limit the character count

text = text:sub(1,3)
1 Like

Oh, figured out the problem. I needed to use a colon : instead of a dot when splitting.

local dist = Plr:GetDistanceFromCharacter(HRP)
dist = tostring(dist):split(".")[1]
print(dist.." studs")

I’ve already tried math.floor. Does not work lol

One word: Impossible.

Try math.ceil or math.round. If you’re desperate, try math.modf.

Already got it to work. No more replies please

1 Like