Jump to content

[HELP] Cruise


K4stic

Recommended Posts

Problem: then i use the Cruise it's set in one sec the speed i set and no works the brake and handbrake

what i need: to be work the brake and handbrake and to get the speed who i set as normal then you drive vehicle

--Client side

-- change it to false to allow all vehicles to use cruise control (seems stupid for bmx etc) 
limit = true 
  
key = "c" 
  
  
allowedTypes = { "Automobile", "Bike", "Boat", "Train", "Helicopter", "BMX", "Quad" } 
  
  
  
function getElementSpeed(element,unit) 
    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.61 * 100 
        end 
    else 
        outputDebugString("Not an element. Can't get speed") 
        return false 
    end 
end 
  
function setElementSpeed(element, unit, speed) -- only work if element is moving! 
    if (unit == nil) then unit = 0 end 
    if (speed == nil) then speed = 0 end 
    speed = tonumber(speed) 
    local acSpeed = getElementSpeed(element, unit) 
    if (acSpeed~=false) then -- if true - element is valid, no need to check again 
        local diff = speed/acSpeed 
        local x,y,z = getElementVelocity(element) 
        setElementVelocity(element,x*diff,y*diff,z*diff) 
        return true 
    else 
        return false 
    end 
end 
  
function in_array(e, t) 
    for _,v in pairs(t) do 
        if (v==e) then return true end 
    end 
    return false 
end 
  
function round2(num, idp) 
  return tonumber(string.format("%." .. (idp or 0) .. "f", num)) 
end 
  
  
function angle(vehicle) 
    local vx,vy,vz = getElementVelocity(vehicle) 
    local modV = math.sqrt(vx*vx + vy*vy) 
  
    if not isVehicleOnGround(vehicle) then return 0,modV end 
  
    local rx,ry,rz = getElementRotation(vehicle) 
    local sn,cs = -math.sin(math.rad(rz)), math.cos(math.rad(rz)) 
  
    -- if modV <= 0.2 then return 0,modV end --speed over 40 km/h 
  
    local cosX = (sn*vx + cs*vy)/modV 
    --if cosX > 0.966 or cosX < 0 then return 0,modV end --angle between 15 and 90 degrees 
    return math.deg(math.acos(cosX))*0.5, modV 
end 
  
lp = getLocalPlayer() 
ccEnabled = false 
  
myveh = false 
targetSpeed = 1 
multiplier = 1 
  
function cc() 
    if (not isElement(myveh)) then 
        removeEventHandler("onClientRender", getRootElement(), cc) 
        ccEnabled=false 
        outputChatBox("** Cruise Control #ff0000disabled", 255,255,255,true) 
        return false 
    end 
    local x,y = angle(myveh) 
    -- outputChatBox(x) 
    if (x<15) then 
        local speed = getElementSpeed(myveh) 
        local targetSpeedTmp = speed + multiplier 
        if (targetSpeedTmp > targetSpeed) then 
            targetSpeedTmp = targetSpeed 
        end 
        if (targetSpeedTmp > 3) then 
            setElementSpeed(myveh, "k", targetSpeedTmp) 
        end 
    end 
end 
  
bindKey(key, "up", function() 
    local veh = getPedOccupiedVehicle(lp) 
    if (veh) then 
        if (lp==getVehicleOccupant(veh)) then 
            myveh = veh 
            if (ccEnabled) then 
                removeEventHandler("onClientRender", getRootElement(), cc) 
                ccEnabled=false 
                outputChatBox("** Cruise Control #ff0000disabled", 255,255,255,true) 
            else 
                targetSpeed = getElementSpeed(veh) 
                if targetSpeed > 4 then 
                    if (limit) then 
                        if in_array(getVehicleType(veh), allowedTypes) then 
                            targetSpeed = round2(targetSpeed) 
                            outputChatBox("** Cruise Control #00ff00enabled at #ffff00"..targetSpeed.."km/h", 255,255,255,true) 
                            addEventHandler("onClientRender", getRootElement(), cc) 
                            ccEnabled=true 
                        end 
                    else 
                        targetSpeed = round2(targetSpeed) 
                        outputChatBox("** Cruise Control #00ff00enabled #ffffff at #ffff00"..targetSpeed.."km/h", 255,255,255,true) 
                        addEventHandler("onClientRender", getRootElement(), cc) 
                        ccEnabled=true 
                    end 
                end 
            end 
        end 
    end 
end) 
  
addEventHandler("onClientPlayerVehicleExit", getLocalPlayer(), function(veh, seat) 
    if (seat==0) then 
        if (ccEnabled) then 
            removeEventHandler("onClientRender", getRootElement(), cc) 
            ccEnabled=false 
            outputChatBox("** Cruise Control #ff0000disabled", 255,255,255,true) 
        end 
    end 
end) 
  

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