Jump to content

Help with string manipulation


jkub

Recommended Posts

Im making a compact speedometer script for experimentational purposes. I know there are is already a selection of speedometers uploaded but I want to learn a basic one myself. I calculated the speed and other things correctly but my main problem here is that I am almost clueless when it comes to manipulating strings. I have did something quite similar to this before and now instead of a "minute.second" format im gonna need a mph format for example "248" Right now I just get "2.48"

Ive tried looking at string.sub and string.format in the lua website but I cant get much out of it. However the only thing I am or seem to be understanding is the number that I put in will change the amount of visible charecters. For example

"speedStr = string.format ( "%.2f", speedSub )" that two allows me to get somthing like this 2.48 where if I changed it to 3 I would get something like 2.485(some other number)

It says there I can use different letters and only two charecters are for strings. I have no clue what each of them does all though Ive tried all of them to see if I could get lucky... No luck

Here is some of the code

function calculateSpeedAndHealth()
local clientVeh = getPedOccupiedVehicle ( getLocalPlayer() )
local velX, velY, velZ = getElementVelocity ( clientVeh )
local realSpeed = ( velX^2 + velY^2 + velZ^2 )^( 0.5 )
--
local vehHealth = getElementHealth ( clientVeh )
--
speedSub = string.sub ( realSpeed, 1, 4 )
speedStr = string.format ( "%.2f", speedSub )
healthString = string.sub ( vehHealth, 1, 3 )
guiSetText ( speedNumber, "Mph: " ..speedStr )
if ( vehHealth >= 1000 ) then
guiSetText ( healthNumber, "Health: 1000" )
else
guiSetText ( healthNumber, "Health: " ..healthString )
end
if isPedInVehicle ( getLocalPlayer() ) then
--
else
guiSetVisible ( speedometer, false )
removeEventHandler ( "onClientRender", getRootElement(), calculateSpeedAndHealth )
end
end

You can also see I used string.sub which from what I understands does ALMOST the same thing as that number I mentioned that changed the ammount of numbers

Please help me with this :?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...