Jump to content

[HELP] Trying to set speed of vehicle


Backsage

Recommended Posts

Ok, so I looked up getElementSpeed and setElementSpeed. I put their function source in a server.lua file and now when I try setting the speed from the command, I get

[2016-03-17 20:00:11] ERROR: test2\server.lua:5: Bad argument 2 @ getElementSpeed (invalid speed unit)

The code is server side.

  
function getElementSpeed(theElement, unit) 
    -- Check arguments for errors 
    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)") 
    -- Default to m/s if no unit specified and 'ignore' argument type if the string contains a number 
    unit = unit == nil and 0 or ((not tonumber(unit)) and unit or tonumber(unit)) 
    -- Setup our multiplier to convert the velocity to the specified unit 
    local mult = (unit == 0 or unit == "m/s") and 50 or ((unit == 1 or unit == "km/h") and 180 or 111.84681456) 
    -- Return the speed by calculating the length of the velocity vector, after converting the velocity to the specified unit 
    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 
  
addCommandHandler("setmyspeed", 
function (player, cmd, arg1) 
  local veh = getPedOccupiedVehicle(player) 
  if (veh) then 
    setElementSpeed(veh, "kph", tonumber(arg1)) 
  else 
    outputChatBox("You have to sit in vehicle", player) 
  end 
end 
) 
  

And when I change it to a number

addCommandHandler("setmyspeed", 
function (player, cmd, arg1) 
  local veh = getPedOccupiedVehicle(player) 
  if (veh) then 
    setElementSpeed(veh, 2, tonumber(arg1)) 
  else 
    outputChatBox("You have to sit in vehicle", player) 
  end 
end 
) 

I get

[2016-03-17 20:25:58] WARNING: test2\server.lua:23: Bad argument @ 'setElementVelocity' [Expected number, got NaN]

Edited by Guest
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...