Jump to content

getElementSpeed not working


hackermagus

Recommended Posts

Hi

I made a script

local vehicle = createVehicle(596, 2392.09009, -1729.73193, 13.38281, 0, 0, 90)
local ped = createPed(281, 2400, -1735, 13.38281)

local ownSpeed = getElementSpeed(vehicle, 1)

setVehicleSirensOn(vehicle, true)

warpPedIntoVehicle(ped, vehicle)


setPedControlState(ped, "accelerate", true)

if (ownSpeed > 29) then
    setPedControlState(ped, "brake_reverse", true)
end

And when I start the game in debugscript 3 it says "attempt to call global 'getElementSpeed' (a nil value)'"

Link to comment
12 minutes ago, stPatrick said:

Because getElementSpeed is an 'useful' function.

You need to paste the function's source into your code.

 

local vehicle = createVehicle(596, 2392.09009, -1729.73193, 13.38281, 0, 0, 90)
local ped = createPed(281, 2400, -1735, 13.38281)

setVehicleSirensOn(vehicle, true)

warpPedIntoVehicle(ped, vehicle)


setPedControlState(ped, "accelerate", true)


addEventHandler("onClientResourceStart", root,
function ()
    local ownSpeed = getElementSpeed(vehicle, 1)
end)

Like this? This not work to

Link to comment
  • Moderators

Nope.

 

https://wiki.multitheftauto.com/wiki/GetElementSpeed

function getElementSpeed(theElement, unit)
    -- Check arguments for errors
    assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")")
    local elementType = getElementType(theElement)
    assert(elementType == "player" or elementType == "ped" or elementType == "object" or elementType == "vehicle" or elementType == "projectile", "Invalid element type @ getElementSpeed (player/ped/object/vehicle/projectile expected, got " .. elementType .. ")")
    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


local vehicle = createVehicle(596, 2392.09009, -1729.73193, 13.38281, 0, 0, 90)
local ped = createPed(281, 2400, -1735, 13.38281)

local ownSpeed = getElementSpeed(vehicle, 1)

setVehicleSirensOn(vehicle, true)

warpPedIntoVehicle(ped, vehicle)


setPedControlState(ped, "accelerate", true)

if (ownSpeed > 29) then
    setPedControlState(ped, "brake_reverse", true)
end

Like this

Link to comment
6 minutes ago, stPatrick said:

Nope.

 

https://wiki.multitheftauto.com/wiki/GetElementSpeed


function getElementSpeed(theElement, unit)
    -- Check arguments for errors
    assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")")
    local elementType = getElementType(theElement)
    assert(elementType == "player" or elementType == "ped" or elementType == "object" or elementType == "vehicle" or elementType == "projectile", "Invalid element type @ getElementSpeed (player/ped/object/vehicle/projectile expected, got " .. elementType .. ")")
    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


local vehicle = createVehicle(596, 2392.09009, -1729.73193, 13.38281, 0, 0, 90)
local ped = createPed(281, 2400, -1735, 13.38281)

local ownSpeed = getElementSpeed(vehicle, 1)

setVehicleSirensOn(vehicle, true)

warpPedIntoVehicle(ped, vehicle)


setPedControlState(ped, "accelerate", true)

if (ownSpeed > 29) then
    setPedControlState(ped, "brake_reverse", true)
end

Like this

Okay, I don't get any error message but the vehicle is currently go faster then 30 km/h.

I try

setPedControlState(ped, "accelerate", false)

But it's not help.

Link to comment
  • Moderators
8 minutes ago, hackermagus said:

Like a for loop?

Not completely.

Your code runs once when you start the resource.

But, every ~0.5-1th seconds you should check how fast the car.

setTimer

function getElementSpeed(theElement, unit)
    -- Check arguments for errors
    assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")")
    local elementType = getElementType(theElement)
    assert(elementType == "player" or elementType == "ped" or elementType == "object" or elementType == "vehicle" or elementType == "projectile", "Invalid element type @ getElementSpeed (player/ped/object/vehicle/projectile expected, got " .. elementType .. ")")
    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


local vehicle = createVehicle(596, 2392.09009, -1729.73193, 13.38281, 0, 0, 90)
local ped = createPed(281, 2400, -1735, 13.38281)

setVehicleSirensOn(vehicle, true)

warpPedIntoVehicle(ped, vehicle)


setPedControlState(ped, "accelerate", true)

setTimer(function()
  if (getElementSpeed(vehicle, 1) > 29) then
      setPedControlState(ped, "brake_reverse", true)
  else
      setPedControlState(ped, "brake_reverse", false)
  end
end, 500, 0) -- check every 0.5s

 

Edited by stPatrick
Link to comment
function CreatePedInVehicle(...)
vehicle_ = createVehicle(596,... ) ped_ = createPed(281,Vector3(getElementPosition ( vehicle_ )))
warpPedIntoVehicle(ped_, vehicle_) setVehicleSirensOn(vehicle_, true)
return vehicle_ , ped_
end
vehicle,ped = CreatePedInVehicle(2392, -1729, 13,0, 0, 90)
setTimer(triggerClientEvent, 100,0,'warp',ped,ped) 
addEvent ( 'warp', true )
addEventHandler ( 'warp', root, function (ped)
setPedAnalogControlState(ped, 'accelerate',0.03)
--setPedAnalogControlState( ped, 'vehicle_left', 1 )
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...