Problems with GetVehicleType Native

Wonderful day to ya,

I’m writing a script in which I want to use GetVehicleType native. Unfortunately when using the Lua function I get a attempt to call a nil value (global GetVehicleType).

As I’m can’t seem to find a solution for this I attempt to call the native by invoking it like the following

local gameVehicleType = Citizen.InvokeNative(0xA273060E, Citizen.PointerValueIntInitialized(vehicle))

Unfortunately I can’t find a function to turn the returned char pointer to a Lua string to then check for a specific value

if gameVehicleType == 'automobile' then
    doSomething()
end

I’m pleased with all your advice :wink:

You seem to be doing something wrong when getting the vehicle.
It retuns nil because it can’t find it.

For testing purposes you could define the vehicle that you are sitting in.

local ped = PlayerPedId()
local veh = GetVehiclePedIsIn(ped, false)
local type = GetVehicleType(veh)

if type == "automobile" then
   --do stuff
end

@Re1ease1 Unfortunately that is not the problem. I already tested for null on the vehicle. It is the GetVehicleType function that is null. (My code looks a lot like your example)

Someone on the Discord told me to try Citizen.ResultAsString() for the InvokeNative() variant. I will let you know how it goes.

Well Citizen.ResultAsString() does not seem to do the trick. My game crashes when running the script :smiley:

local gameVehicleType = Citizen.InvokeNative(0xa273060e, vehicle, Citizen.ResultAsString())

Ok I got behind what the problem was. GetVehicleType() is a server-side function and I was trying to call it client-side. TBH I don’t know why this is a server-side only function as it is pretty hard to get the vehicle on the server-side.

Well thanks for your help.

Good point, this could be made available on client as a helper-ish.

Alternately:

combined with

etc.

1 Like

I know this is solved, but an alternative solution that I find best, hadn’t been suggested. GetVehicleClass is a client function that returns an integer that references the vehicle type. Not just being a car, but the category of car too. However it also detects if they are bikes, trains, planes, etc.

If you want to do things for different vehicle types, even different CAR types, instead of using IsThisModelACar, IsThisModelABike, etc. you can call just one function and do things based on the integer it returns. Hope this helps!

Of note is that this doesn’t actually return the type but a different field, so if a vehicles.meta is misconfigured, this may call a bike a plane and a car a helicopter, despite them actually being a bike and a car.

@nta I will look into your suggestion when I have time.

My script currently uses GetVehicleClass() but for me it is a little bit too ‘inaccurate’ as I don’t know if maybe a military plane will be in the class planes or in military. For now it works ok but a GetVehicleType() client-side would be really nice.

All helicopters are helicopters, all planes are planes. Military will be military cars, trucks & tanks. The military aeronautical vehicles are classed as normal helis and planes. The only time this might not be the case, will be custom addon vehicles. In which case, as d-bubble pointed out, it can be checked in their respective vehicles.meta files. If it’s the wrong class, you can easily change it.

This has helped me SOO sooo much! Thank you!

sorry if this is late just got back into coding FiveM mods
you want to do it client use this
I put mine into the core so i can use it throughout the server

local VehicleCategory = {}

local function GetVehicleCategory(class)
    return VehicleCategory[class]
end

CreateThread(function()
    for category, classes  in pairs(VehicleCategories) do
        for _, class  in pairs(classes) do
            VehicleCategory[class] = category
        end
    end
end)

VehicleCategories = {
    ['car'] = {0,1,2,3,4,5,6,7,9,10,11,12},
    ['motorcycle'] = {8},
    ['boat'] = {14},
    ['helicopter'] = {15},
    ['plane'] = {16},
    ['service'] = {17},
    ['emergency'] = {18},
    ['other'] = {13},
	['custom'] = {your custom class}
}

to use it just do

local vehclass = GetVehicleClass(veh)
local vehcategory = GetVehicleCategory(vehclass)
-- Set example for garage... each vehcategories defined in garages config
if currentgarage and not TableContains(Garages[currentgarage].vehcategories, vehcategory ) then
    -- do stuff 
else
    print("Incorrect Vehicle Category for this garage")
end
1 Like

** Client **
local veh = GetVehiclePedIsIn(PlayerPedId(), false)
local netId = VehToNet(veh)

** Server **
local entity = NetworkGetEntityFromNetworkId(netId)
local vehicleType = GetVehicleType(entity)

automobile, bike, boat, heli, plane, submarine and trailer