Jump to content

0-100km/h script


Piciu713

Recommended Posts

Posted

Hello.

I need a small script, to measure time how long does it take for a car to reach 100km/h from standing still.

I have no idea where to start with this...

Thanks in advance

Posted

You could check the tick count when the player starts driving, and then while his speed is lower than 101 you check if his speed is 100, incase that's true, you break the loop and check the tickcount again.

Something like this

  
function getElementSpeed(element,unit) -- From the wiki, [url=https://wiki.multitheftauto.com/wiki/GetElementSpeed]https://wiki.multitheftauto.com/wiki/GetElementSpeed[/url] 
    if (unit == nil) then unit = 0 end 
    if (isElement(element)) then 
        local x,y,z = getElementVelocity(element) 
        if (unit=="mph" or unit==1 or unit =='1') then 
            return (x^2 + y^2 + z^2) ^ 0.5 * 100 
        else 
            return (x^2 + y^2 + z^2) ^ 0.5 * 1.8 * 100 
        end 
    else 
        outputDebugString("Not an element. Can't get speed") 
        return false 
    end 
end 
  
--Start these when the player starts driving 
while getElementSpeed(player) < 2 do 
    if getElementSpeed(player) == 1 then 
        curCount = getTickCount() 
    end 
end 
  
while getElementSpeed(player) < 101 do 
    if getElementSpeed(player) == 100 then 
        newCount = getTickCount() 
        break 
    end 
end 
  
speedupTime = (newCount - curCount)/1000 
  

(I've never used while loops though, so I'm not 100% sure whether or not it works.)

Posted
  
local SquareRoot = math.sqrt; 
local GetSpeed = function(element) 
    local Velocity = {getElementVelocity(element)}; 
    return SquareRoot(Velocity[1]^2+Velocity[2]^2+Velocity[3]^2)*180; 
end; 
  
addCommandHandler("speed",function() 
    local Vehicle = getPedOccupiedVehicle(localPlayer); 
    if Vehicle and GetSpeed(Vehicle)<=0 then 
        local Timer,StartTick = nil,nil; 
        Timer = setTimer(function() 
            if GetSpeed(Vehicle)>0 then 
                killTimer(Timer); 
                StartTick = getTickCount(); 
                Timer = setTimer(function() 
                    if GetSpeed(Vehicle)>=100 then 
                        killTimer(Timer); 
                        outputChatBox("Test successful! Time elapsed - "..((getTickCount()-StartTick)/1000).." seconds;",255,0,0); 
                    end; 
                end,50,0); 
                outputChatBox("The speed test started!",0,255,0); 
            end; 
        end,50,0); 
        outputChatBox("Start accelerating when you wish to begin!",0,255,0); 
    else 
        outputChatBox("You firstly have to enter a vehicle and not move!",255,0,0); 
    end; 
end); 
  

You just call the command /speed when you are in a vehicle.

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...