Jump to content

cruise


MAB

Recommended Posts

Posted

i am making a speed cruise thats when you press W+C it turns on and drives the vehicle on the speed you pressed the button in. not working... if you get a better idea than mine to make a cruise please share.

client :

function getElementSpeed(theElement, unit) 
    assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")") 
    assert(getElementType(theElement) == "player" or getElementType(theElement) == "ped" or getElementType(theElement) == "object" or getElementType(theElement) == "vehicle", "Invalid element type @ getElementSpeed (player/ped/object/vehicle expected, got " .. getElementType(theElement) .. ")") 
    assert((unit == nil or type(unit) == "string" or type(unit) == "number") and (unit == nil or (tonumber(unit) and (tonumber(unit) == 0 or tonumber(unit) == 1 or tonumber(unit) == 2)) or unit == "m/s" or unit == "km/h" or unit == "mph"), "Bad argument 2 @ getElementSpeed (invalid speed unit)") 
    unit = unit == nil and 0 or ((not tonumber(unit)) and unit or tonumber(unit)) 
    local mult = (unit == 0 or unit == "m/s") and 50 or ((unit == 1 or unit == "km/h") and 180 or 111.84681456) 
    return (Vector3(getElementVelocity(theElement)) * mult).length 
end 
  
function setElementSpeed(element, unit, speed) 
    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 
        if diff ~= diff then return end -- if the number is a 'NaN' return end. 
        local x,y,z = getElementVelocity(element) 
        setElementVelocity(element,x*diff,y*diff,z*diff) 
        return true 
    end 
  
    return false 
end 
  
function stoping () 
    if isPedInVehicle(localPlayer) then 
        if getElementData(localPlayer,"cruise") ~= false then 
           local veh = getPedOccupiedVehicle(localPlayer) 
           local speed = getElementSpeed(veh,1) 
           local down = speed - 1 
           local data = getElementData(localPlayer,"cruise") 
           setAnalogControlState("accelerate",1) 
            if speed > data then 
               setElementSpeed( veh, 1, down ) 
            end 
        else 
           setAnalogControlState("accelerate",0) 
        end 
    end 
end 
addEventHandler("onClientRender",root,stoping) 
  
function toggle_cruise (key,keyState) 
    if isPedInVehicle(localPlayer) then 
        local veh = getPedOccupiedVehicle(localPlayer) 
        if ( getElementModel(veh) ~= 510 and getElementModel(veh) ~= 509 and getElementModel(veh) ~= 481 ) then 
            if getPedOccupiedVehicleSeat(localPlayer) == 0 then 
                if getKeyState ("w") == true then 
                    local data = getElementData(localPlayer,"cruise") 
                    if data == false then 
                        local speed = getElementSpeed(veh,1) 
                        if speed > 30 then 
                            setElementData(localPlayer,"cruise",tostring(speed)) 
                        end 
                    else 
                        setElementData(localPlayer,"cruise",false) 
                    end 
                end 
            end 
        end  
    end 
end 
bindKey("c","down",toggle_cruise) 
  
local controls = { "fire", "next_weapon", "previous_weapon", "forwards", "backwards", "left", "right", "zoom_in", "zoom_out", 
 "change_camera", "jump", "sprint", "look_behind", "crouch", "action", "walk", "aim_weapon", "conversation_yes", "conversation_no", 
 "group_control_forwards", "group_control_back", "enter_exit", "vehicle_fire", "vehicle_secondary_fire", "vehicle_left", "vehicle_right", 
 "steer_forward", "steer_back", "accelerate", "brake_reverse", "radio_next", "radio_previous", "radio_user_track_skip", "horn", "sub_mission", 
 "handbrake", "vehicle_look_left", "vehicle_look_right", "vehicle_look_behind", "vehicle_mouse_look", "special_control_left", "special_control_right", 
 "special_control_down", "special_control_up" } 
  
local boundControlsKeys = {} 
local bindsData = {} 
  
function unbindControlKeys(control) 
    -- Ensure the argument has got the appropiate type 
    assert(type(control) == "string", "Bad argument @ unbindControlKeys [string expected, got " .. type(control) .. "]") 
    -- Check if we have a valid control 
    local validControl 
    for _, controlComp in ipairs(controls) do 
        if control == controlComp then 
            validControl = true 
            break 
        end 
    end 
    assert(validControl, "Bad argument @ unbindControlKeys [invalid control name]") 
    -- Have we got a bind on this control? 
    assert(boundControlsKeys[control], "Bad argument @ unbindControlKeys [There is no bind on such control]") 
    -- Unbind each key of the control 
    for _, bindData in pairs(bindsData[control]) do 
        unbindKey(unpack(bindData)) 
    end 
    -- Remove references 
    boundControlsKeys[control] = nil 
    bindsData[control] = nil 
    return true 
end 
  
function bindControlKeys(control, ...) 
    -- Ensure the argument has got the appropiate type 
    assert(type(control) == "string", "Bad argument 1 @ bindControlKeys [string expected, got " .. type(control) .. "]") 
    -- Check if we have a valid control 
    local validControl 
    for _, controlComp in ipairs(controls) do 
        if control == controlComp then -- Is the specified control in the table? 
            validControl = true -- If so, it's a valid control 
            break 
        end 
    end 
    assert(validControl, "Bad argument 1 @ bindControlKeys [invalid control name]") 
    -- Do we already have this control bound? 
    if boundControlsKeys[control] then 
        unbindControlKeys(control) -- Delete the first control keys bind 
    end 
    boundControlsKeys[control] = getBoundKeys(control) -- Store the keys of that control that will be bound 
    bindsData[control] = {} -- Store bind data, so we can unbind each key of the control later 
    for key in pairs(boundControlsKeys[control]) do 
        -- Can we bind the key with the specified arguments? 
        assert(bindKey(key, unpack(arg)), "Bad arguments @ bindControlKeys [Could not create key bind]") 
        -- If so, register the bind data and continue 
        table.insert(bindsData[control], { key, unpack(arg) }) 
    end 
    return true 
end 
  
function off () 
    if isPedInVehicle(localPlayer) then 
        if getElementData(localPlayer,"cruise") ~= false then 
           setElementData(localPlayer,"cruise",false) 
        end 
    end 
end 
bindControlKeys("brake_reverse","down",off) 
bindControlKeys("handbrake","down",off) 

server :

function out (player) 
setElementData(player,"true",false) 
    if getElementData(player,"cruise") ~= false then 
       setElementData(player,"cruise",false) 
    end 
end 
addEventHandler ("onVehicleExit",getRootElement(),out) 
  

Posted
You could try to use controlstates instead.
setControlState 

the problem is that he don't low the speed when it reach the target speed... not it don't move...

i am sorry that i wasn't clear from the beginning

Posted
When you set the control to false, aren't you lowering your speed because you stop accelerating?

nvm i solved it

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