Jump to content

Please help me with this table problem


jingzhi

Recommended Posts

if getElementSpeed(for id, vehicle in ipairs(getElementsByType("vehicle"))) > 120 

I want to make this script to determine if a vehicle has speed above 120 kmh, but getElementByType(vehicle) give me a table, so how should i write this? I am noob and didn't know all these commands and arguments for table yet, so please please explain me in detail, thank you very very much :)

Link to post
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 
  
for id, vehicle in ipairs(getElementsByType("vehicle")) do 
    if getElementSpeed(vehicle, 1) > 120 then 
    --[...] 
    end 
end 

Link to post

There you are:

local vehTable = getElementsByType("vehicle") 
  
setTimer(function() 
    for k, v in ipairs(vehTable) do 
        if getElementSpeed(v, 1) > 120 then 
            outputChatBox(getVehicleName(v).." is now at speed greater than 120 KM/H!") 
        end 
    end 
end, 3000, 0) 

However you need to grab one "Useful function" from wiki which is:

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 

Add both those codes in your script's client side and start it, the function in first code will check speed of every vehicle in server every 3 seconds and will output a message to player alogwith vehicle name (I made it so that you can get to know the table better :))

Link to post
There you are:
local vehTable = getElementsByType("vehicle") 
  
setTimer(function() 
    for k, v in ipairs(vehTable) do 
        if getElementSpeed(v, 1) > 120 then 
            outputChatBox(getVehicleName(v).." is now at speed greater than 120 KM/H!") 
        end 
    end 
end, 3000, 0) 

However you need to grab one "Useful function" from wiki which is:

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 

Add both those codes in your script's client side and start it, the function in first code will check speed of every vehicle in server every 3 seconds and will output a message to player alogwith vehicle name (I made it so that you can get to know the table better :))

Thank you very much Ryan, may I ask one more question : "for k,v in ipairs(vehTable) do" what this code means? What does k,v and ipairs means? :)

Link to post

The table isn't numeric index therefore you should use pairs instead of ipairs.

about your question

k = iteration number

v = value of the table

it can be also like this

  
-- i or k it's the same 
for i , v in pairs (getElementsByType("vehicle")) do 
-- i is the index and v  it's value 
-- for pairs instead of "i" I usually use "k" wich means key (of the value). 
  

Link to post
The table isn't numeric index therefore you should use pairs instead of ipairs.

about your question

k = iteration number

v = value of the table

it can be also like this

  
-- i or k it's the same 
for i , v in pairs (getElementsByType("vehicle")) do 
-- i is the index and v  it's value 
-- for pairs instead of "i" I usually use "k" wich means key (of the value). 
  

thank you very much :)

Link to post

thank you very much :)

Actually K and V can be any variable:

for cat , dog in pairs (getElementsByType("vehicle")) do 

for mta , gta in pairs (getElementsByType("vehicle")) do 

for jingzhi , example in pairs (getElementsByType("vehicle")) do 

But people use single words, mostly K and V for keys and values, actually we are assigning a name to key part and values part from a table like we usually do:

local k = key 
local v = value 

... etc

Link to post

thank you very much :)

Actually K and V can be any variable:

for cat , dog in pairs (getElementsByType("vehicle")) do 

for mta , gta in pairs (getElementsByType("vehicle")) do 

for jingzhi , example in pairs (getElementsByType("vehicle")) do 

But people use single words, mostly K and V for keys and values, actually we are assigning a name to key part and values part from a table like we usually do:

local k = key 
local v = value 

... etc

is "k" representing the variable of the name of one item in the table, and v showing it's value?

Link to post
Some sort of this

:~ = { "yo", "yo2" }

When it's at yo it will return 1 and when at yo2 it will return 2. Remember ipairs is only for indexed tables.

And v will show the value yo and yo2.

Thank you very much, getting it now :)

Link to post

thank you very much :)

Actually K and V can be any variable:

for cat , dog in pairs (getElementsByType("vehicle")) do 

for mta , gta in pairs (getElementsByType("vehicle")) do 

for jingzhi , example in pairs (getElementsByType("vehicle")) do 

But people use single words, mostly K and V for keys and values, actually we are assigning a name to key part and values part from a table like we usually do:

local k = key 
local v = value 

... etc

Thank you :)

Link to post

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