Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how can i use string.sub to only display the main numbers of a script's local value?

i'm trying to make a physical text speedometer for my car, and i'm using string.sub to only show the main values of the speed in km/h, and i'm making it update if the main numbers are 1 digit, 2 digits or 3 digits, by using three separate string.sub's, but it doesn't properly work, because sometimes it doesnt switch properly, as the values change incorrectly, can i have any help with getting this to correctly work? any help is greatly appreciated!

this is the code

script.Parent.Values.Velocity.Changed:connect(function()
	wait()
	--local sps = script.Parent.Values.Velocity.Value.Magnitude (held as comment to save later)
	local sps = car.DriveSeat.Velocity.Magnitude
	local ips = sps * 10 --inches per second
	local mph = ips * 0.568182 -- miles per hour
	local kmh = mph * 1.60934 --kilometers per hour
	
	local stringnumber = tostring(kmh)
	local speed = string.sub(stringnumber, 0, 1)
	local speed2 = string.sub(stringnumber, 0, 2)
	local speed3 = string.sub(stringnumber, 0, 3)
	local spddis = car.Body.Speed.Gui
	
	script.kmh.Value = kmh/10
	spddis.One.Text = speed
	
	if kmh <= 1 then 
		spddis.Zero.Visible = true
	end
	if kmh <= 9 and kmh >= 1 then
		spddis.Zero.Visible = false
		spddis.One.Text = speed
	end
	if kmh <= 99 and kmh >= 10 then
		spddis.Zero.Visible = false
		spddis.One.Text = speed2
	end
	if kmh <= 1000 and kmh >= 99 then
		spddis.Zero.Visible = false
		spddis.One.Text = speed3
	end
end)

1 answer

✓ Accepted answer

I'm assuming by main numbers you mean the whole number of the speed?

One thing you can do is separate the string into parts, using string.split() (scroll on that page to string.split()).

Some example to hopefully get you going:

local speed = 1234.56 -- studs per second

local speedSeparated = string.split(speed, ".")
--> this stores a table that looks like {"1,234", "56"}

local newSpeed = speedSeparated[1]
--> newSpeed is a string "1,234"

1 comment

0

8mo later, i should've told you to floor it :v

This discussion is preserved as it was. New answers, comments, and votes are closed.