[Help][LUA]Trying to create a SubMenu and have it action when pressing Enter

I’m in the process of creating a menu with NativeUI. I have the menu working, and the sub menu shows as expected. Although I’m not 100% sure how to ‘action’ my presses on the Sub Menu if that makes sense.

I get no errors with the F8 Console, so I have a feeling it’s a simple fix within my if statement.

There’s a video below showing what happens.

function jsocMainMenu(menu)
    --Creates the Main Menu for the Role Select
    local roleSelect = _menuPool:AddSubMenu(menu, "Role Select")
        menu:AddItem(roleSelect)
        --This is the Role Select Sub-Menu (Rifleman, Auto Rifleman, Medic)
        roleSelect:AddItem(NativeUI.CreateItem("Rifleman",""))
        roleSelect:AddItem(NativeUI.CreateItem("Auto Rifleman",""))
        roleSelect:AddItem(NativeUI.CreateItem("Medic",""))
        --Where the Action would take place within the SubMenu
        roleSelect.OnItemSelect = function(sender, item, index)
            if item == roleSelect then
                TriggerEvent("chatMessage", "I Work")
            end
        end
end

If you need anymore info, please let me know.

you need to define the submenu’s items in variables if you want to check which item is selected:
like :

function jsocMainMenu(menu)
    --Creates the Main Menu for the Role Select
    local roleSelect = _menuPool:AddSubMenu(menu, "Role Select")
        menu:AddItem(roleSelect)
        --This is the Role Select Sub-Menu (Rifleman, Auto Rifleman, Medic)
        local first = NativeUI.CreateItem("Rifleman","")
        local second = NativeUI.CreateItem("Auto Rifleman","")
        local third = NativeUI.CreateItem("Medic","")

        roleSelect:AddItem(rifleman)
        roleSelect:AddItem(second)
        roleSelect:AddItem(third)
        --Where the Action would take place within the SubMenu
        roleSelect.OnItemSelect = function(sender, item, index)
            if item == first then
                TriggerEvent("chatMessage", "Rifleman selected")
            elseif item == second then
                TriggerEvent("chatMessage", "Auto Rifleman selected")
            elseif item == third then
                TriggerEvent("chatMessage", "Medic selected")
            end
        end
end
1 Like

Thanks for this. I knew it would be something silly that I forgot to do. Appreciate the response and help!

1 Like