Search code examples
lua

Array element from lua


I'm making a fivem server. But when i tru to pick the job.grade.name he says No grades.

QBShared.Jobs = {
["unemployed"] = {
    label = "Werkloos",
    grades = {
        [0] = {
            name = 'Werkloos',
            payment = 10,
        },
    },
    defaultDuty = true,
},
["police"] = {
    label = "Politie",
    grades = {
        [0] = {
            name = "Politie - Student", **Want to pick this**
            payment = 200,
        },

        [1] = {
            name = 'Aspirant',
            payment = 300,
        },

        [2] = {
            name = 'Agent',
            payment = 400,
        },

        [3] = {
            name = 'Hoofd Agent',
            payment = 400,
        },

        [4] = {
            name = 'Brigadier',
            payment = 400,
        },

        [5] = {
            name = 'Inspecteur',
            payment = 400,
        },

        [6] = {
            name = 'Hoofd Inspecteur',
            payment = 400,
        },

        [7] = {
            name = 'Commissaris',
            payment = 400,
        },

        [8] = {
            name = 'Hoofd Commissaris',
            payment = 400,
        },

        [9] = {
            name = 'Eerste Hoofd Commissaris',
            isboss = true,
            payment = 400,
        },
    },
    defaultDuty = true,

So people can type /baan and then the see Baan: Politie What i want is The must see Baan: Politie - Politie Student

QBCore.Commands.Add("baan", "Kijk wat je baan is", {}, false, function(source, args)
local Player = QBCore.Functions.GetPlayer(source)
TriggerClientEvent('chatMessage', source, "SYSTEM", "warning", "Baan: "..Player.PlayerData.job.label .. ' - ' ..Player.PlayerData.job.grade.name)

end)

Someone can help me? Because i want to learn more about lua but dont get this to work..


Solution

  • 'Jobs' needs a string key to access it
    and 'grades' needs a numerical index

    Player .PlayerData .Jobs [job] .grades [grade] .name
    

    TriggerClientEvent('chatMessage', source, "SYSTEM", "warning", "Baan: "..Player.PlayerData.job.label .. ' - ' ..Player.PlayerData.Jobs[job].grades[grade].name)


    I'm assuming that somehow within your game engine, these values get parsed into PlayerData. That'll depend on the functions contained within fivem, and that you've used them properly. Otherwise, to access the raw table data, it's more like this:

    print( QBShared.Jobs['police'].label )
    

    Politie

    print( QBShared.Jobs['police'].grades[0].name )
    

    Politie - Student

    print( QBShared.Jobs['police'].grades[0].payment )
    

    200


    if the game rearranges those during import into PlayerData, it might be

    Player.PlayerData[job][grade].name

    but it's very likely it remains in the original syntax illustrated above.