Too many digits in TextLabel. Can I limit it?

I made a script to change the text of an ImageLabel to the travelling speed of a part. I am doing this by getting the part’s velocity. It works fine, it’s just that there are WAY too many numbers, I only want the first 2 or so digits. Is there anything I can do to shorten the amount of digits in the box?

Screenshot:

image

My script:

game.ReplicatedStorage.ShipValues.SpeedPower.OnClientEvent:Connect(function(Speed)
	script.Parent.Text = Speed
end)
1 Like

Try this post

Try searching before you ask!

Change the script to:

game.ReplicatedStorage.ShipValues.SpeedPower.OnClientEvent:Connect(function(Speed)
	script.Parent.Text = Speed:Sub(1, 3)
end)

Yes you can get the lengh of a string through string.len() or by using #labelText

example:

local string = textLabel.Text

local lenght = string.len(string)

Other method

local string = textLabel.Text

local lenght = #string

If lenght > number then
    print("Too many characters")

    local newLenght = lenght:Sub(1, 3)
end

And if you want to get a not decimal number you can use math.floor()

I understand your approach, but I get an error as I am “attempting to get length of a number value”.

Is the error at this line of the code?

No, here

local length = #string

Better use math.floor

game.ReplicatedStorage.ShipValues.SpeedPower.OnClientEvent:Connect(function(Speed)
	script.Parent.Text = math.floor(Speed)
end)

if you want decimals

script.Parent.Text = math.floor(Speed*10)/10

It will give this error if you pass the string with tonumber(speed) it has to be a text

Actually he wants to get some of the decimal numbers of the string

If you want an actual rounded value, I recommend multiplying it by the value whose square root is the amount of digits you want to display and then using math.round on it.

local oldSpeed = Speed
local roundedSpeed = math.round(oldSpeed * 100) / 100 -- should round to the hundredth
game.ReplicatedStorage.ShipValues.SpeedPower.OnClientEvent:Connect(function(Speed)
	script.Parent.Text = tostring(Speed):Sub(1, 3)
end)

Did you try to use that script

I forgot about this but you can also use string.format for this.

local str = '$%.3f' -- 3 is the precision
script.Parent.Text = str:format(Speed)