Need help with a for loop/pairs

There has to be better way to do this with loops but I’m not advanced enough. I’m using NativeUI to select a driving style for an autodrive script I’m writing. I have a few driving styles and I want to cycle thru them with the menu. Here’s what I have so far. The menu works, but I’m sure there’s a for loop that would work a lot better than what I’ve come up with. I would like to use pairs/ for loop. The name of the driving style points to a number. Something like this {name = “x” , value = y} Can you help me figure this out? I’m writing this myself and plan to release it. thanks

drivingStyle = {
    "safe", --value = 411},
    "aggressive", --value = 318},
    "wreckless", -- value = 524841},
}
function ThirdItem(menu) -- Driving style menu item
    local drivingStyleList = NativeUI.CreateListItem("Driving Style", drivingStyle, 1)
    menu:AddItem(drivingStyleList)
    menu.OnListSelect = function(sender, item, index)  
        if item == drivingStyleList then
            local style = item:IndexToItem(index)
            if style == "safe" then
                chosenDrivingStyle = 411
            elseif style == "aggressive" then
                chosenDrivingStyle = 318
            elseif style == "wreckless" then
                chosenDrivingStyle = 524841
            end
            notify("chosen "..chosenDrivingStyle .. " " .. index)
        end
    end
end

Hey there!
Actually, for loop and/or in pairs/ipairs would be worse than your example. But you want another way of achieving the same result so here you go.

drivingStyle = {
    ["safe"] = 411,
    ["aggressive"] = 318,
    ["wreckless"] = 524841,
}
function ThirdItem(menu) -- Driving style menu item
    local drivingStyleList = NativeUI.CreateListItem("Driving Style", drivingStyle, 1)
    menu:AddItem(drivingStyleList)
    menu.OnListSelect = function(sender, item, index)  
        if item == drivingStyleList then
            local style = item:IndexToItem(index)
            chosenDrivingStyle = drivingStyle[style] or 411 -- in case of list being nil
            notify("chosen "..chosenDrivingStyle .. " " .. index)
        end
    end
end

This uses string index in table and just searches for it. Its easier to write and quicker, but some people may still be confused by it. Still better than x amount of if,elseif,else.

1 Like

Thanks for the reply. This might be what I was looking for.

I just tried it and it only gives nil. It seems closer to what I want to do. Rather than typing and comparing each string to do something, I want to reference the string and it chooses the value. If “safe” is chosen then it looks up the value and chooses the value.

drivingStyle = {
    ["Safe"] = {name = "Safe", value = 411},
    ["Aggressive"] = {name = "Aggressive", value = 318},
    ["Wreckless"] = {name = "Wreckless", value = 524841},
}

My piece of code should have worked, but in this case, create another table with my layout and use it, reverse changes to the table drivingStyles. It should work now, if it doesn’t then check length of a string and if it doesn’t match then gsub any spaces

1 Like

The code worked, it didn’t throw any errors. In the menu, it would only display nil. I’ve been experimenting and came up with this block. Still not sure how to apply it tho. It works to print out the tables inside the tables. I would like to have between 6-10 driving styles and look them up.

drivingStyle = {
    [1] = {name = "Safe", value = 411},
    [2] = {name = "Aggressive", value = 318},
    [3] = {name = "Wreckless", value = 524841},
}
RegisterCommand("test", function(source, args, rawcommand)
	for k, v in pairs(drivingStyle) do
		for l, b in pairs(v) do
            print(l, b)
        end
    end
end)
output:
value    411
name   Safe
value    318
name    Aggressive
value    524841
name    Wreckless

The problem is not in the table itself, but with NativeUI.

local drivingStyle = {
    "safe",
    "aggressive",
    "wreckless",
}

local values = {
    ["safe"] = 411,
    ["aggressive"] = 318,
    ["wreckless"] = 524841,
}

function ThirdItem(menu) -- Driving style menu item
    local drivingStyleList = NativeUI.CreateListItem("Driving Style", drivingStyle, 1)
    menu:AddItem(drivingStyleList)
    menu.OnListSelect = function(sender, item, index)  
        if item == drivingStyleList then
            local style = item:IndexToItem(index)
            chosenDrivingStyle = values[style] or 411 -- in case of list being nil
            notify("chosen "..chosenDrivingStyle .. " " .. index)
        end
    end
end

This bit of code should work, tell me if it does or doesn’t

1 Like

That worked! I figured it had to be something with nui and getting it into a format that nui can use. Thanks for your help. One step closer to finishing this autodrive script. I’ll be releasing it free when I’m done :slight_smile:

I plan to have several driving styles, drive to waypoint, wander around, native ui support, and a few other things. Next is to figure out a way to listen for blips and then drive to the blip. For example, a police or medical callout and then the script autodrives to it.

Thanks for your help. I ended up scrapping the list because NativeUI was a little wonky. No delay in between key presses and it would skip over items in the list. I settled for submenus and each item has their own activator. Here is a preview of what I have so far. Let me know what you think

https://forum.cfx.re/t/preview-gtav-autodrive-script/4907626?u=punisher0111