luagrand-theft-autofivem

FiveM Vehicle Extra List using NUI


I'm creating a stand-alone vehicle extras menu. The problem I'm having is if I spawn car A and it has 4 extra options, they work fine. But if I spawn car B and it has 8, the menu will still only show 4 from the previous car A. This is true regardless if I leave car A or delete it. Any ideas how to reset that so it reads the correct vehicle?

local AvailableExtras = {['VehicleExtras'] = {}}
local Items = {['Vehicle'] = {}}
local MenuExists, Vehicle
_menuPool = NativeUI.CreatePool()
mainMenu = NativeUI.CreateMenu("Vehicle Extras", "~b~Select Extras")       
_menuPool:Add(mainMenu)
_menuPool:MouseControlsEnabled (false);
_menuPool:MouseEdgeEnabled (false);
_menuPool:ControlDisablingEnabled(false);

Citizen.CreateThread(function()
        while true do
                Citizen.Wait(0)
                _menuPool:MouseControlsEnabled (false);
                _menuPool:MouseEdgeEnabled (false);
                _menuPool:ControlDisablingEnabled(false)
                _menuPool:ProcessMenus()
                if IsControlJustPressed(1, 56) then
                    mainMenu:Visible(not mainMenu:Visible())
                end
        end
end)

function Extras(menu)
    local AvailableExtras = {['VehicleExtras'] = {}}
    local Items = {['Vehicle'] = {}}
    local MenuExists
    local Vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
    local GotVehicleExtras = false
    for ExtraID = 0, 20 do
            if DoesExtraExist(Vehicle, ExtraID) then
                    AvailableExtras.VehicleExtras[ExtraID] = (IsVehicleExtraTurnedOn(Vehicle, ExtraID) == 1)
                    GotVehicleExtras = true
            end
    end
    -- Vehicle Extras
    if GotVehicleExtras then
            for Key, Value in pairs(AvailableExtras.VehicleExtras) do
                    local ExtraItem = NativeUI.CreateCheckboxItem('Extra ' .. Key, AvailableExtras.VehicleExtras[Key],Vehicle)
                    mainMenu:AddItem(ExtraItem)
                    Items.Vehicle[Key] = ExtraItem
            end
            menu.OnCheckboxChange = function(Sender, Item, Checked)
                    for Key, Value in pairs(Items.Vehicle) do
                            if Item == Value then
                                    AvailableExtras.VehicleExtras[Key] = Checked
                                    if AvailableExtras.VehicleExtras[Key] then
                                            SetVehicleExtra(Vehicle, Key, 0)
                                    else
                                            SetVehicleExtra(Vehicle, Key, 1)
                                    end
                            end
                    end
            end
    end
end
Extras(mainMenu)
_menuPool:RefreshIndex()

Solution

  • Fixed it

            while true do
                    Citizen.Wait(0)
                    _menuPool:MouseControlsEnabled (false);
                    _menuPool:MouseEdgeEnabled (false);
                    _menuPool:ControlDisablingEnabled(false)
                    _menuPool:ProcessMenus()
                    if IsControlJustPressed(1, 56) then
                        Extras(mainMenu)
                        mainMenu:Visible(not mainMenu:Visible())
                    end
            end
    end)
    
    function Extras(menu)
        mainMenu:Clear()
        local AvailableExtras = {['VehicleExtras'] = {}}
        local Items = {['Vehicle'] = {}}
        local MenuExists
        local Vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
        local GotVehicleExtras = false
        for ExtraID = 0, 20 do
                if DoesExtraExist(Vehicle, ExtraID) then
                        AvailableExtras.VehicleExtras[ExtraID] = (IsVehicleExtraTurnedOn(Vehicle, ExtraID) == 1)
                        GotVehicleExtras = true
                end
        end
        -- Vehicle Extras
        if GotVehicleExtras then
                for Key, Value in pairs(AvailableExtras.VehicleExtras) do
                        local ExtraItem = NativeUI.CreateCheckboxItem('Extra ' .. Key, AvailableExtras.VehicleExtras[Key],"Enable or Disable Extras")
                        mainMenu:AddItem(ExtraItem)
                        Items.Vehicle[Key] = ExtraItem
                end
                menu.OnCheckboxChange = function(Sender, Item, Checked)
                        for Key, Value in pairs(Items.Vehicle) do
                                if Item == Value then
                                        AvailableExtras.VehicleExtras[Key] = Checked
                                        if AvailableExtras.VehicleExtras[Key] then
                                                SetVehicleExtra(Vehicle, Key, 0)
                                        else
                                                SetVehicleExtra(Vehicle, Key, 1)
                                        end
                                end
                        end
                end
        end
    end```