Seeking an example of AddStateBagChangeHandler()

Hi all,

I must be a smoll brain today because nothing I am doing is working all that well.

I am seeing if anyone has a working example, other than ■■■■■■■■■’s C# implementation of it.

I’d prefer it in lua or JS just so I can read along fluently, if anyone could run me though a basic example I would appreciate it.

Many thanks in advance.

T

I rarely venture into the cfx.re discord.

But when I do, I use the search feature.

Many thanks to @AvarianKnight for assisting someone there with it - only tagged as it is a direct copy paste from discord of their example.

Hopefully this helps others at some point.

AddStateBagChangeHandler('staticVehicleCoords', nil, function(bagName, key, value, _, _)
    local startedTask = false
    local entNet = bagName:gsub('entity:', '') -- just gets the net id
    if not value then return end
    -- value should be a vec3 with target coords
    while NetworkDoesEntityExistWithNetworkId(entNet) do
        local veh = NetToVeh(entNet)
        if NetworkGetEntityOwner(veh) == PlayerId()  then
            if not startedTask then
                startedTask = true
                TaskVehicleDriveToCoordLongrange(GetPedInVehicleSeat(veh, -1), veh, value, 1.0, 1, 1)
            end
        else
            startedTask = false
        end

        Wait(250)
    end
end)
1 Like