Lua table pairs output showing same value

92 views Asked by At

im fairly new to lua and working on CFX FiveM, and have made a simple table.insert which calls on a for type, value in pairs(Config.Skills) do This is then refering back to a SQL table which is pulling the data correctly, but the table inserts are showing the same value.

SQL OUTPUT - {"stamina":{"RemoveAmount":-0.3,"Stat":"MP0_STAMINA","Current":25},"strength":{"RemoveAmount":-0.3,"Current":13,"Stat":"MP0_STRENGTH"}}

Lua Code

function SkillMenu()
    FetchSkills()
    Wait(1000)
    RefreshSkills()
    local elements = {}
    for type, value in pairs(Config.Skills) do
        table.insert(elements, {
            type = 'button',
            name = "stamina",
            label = "Stamina",
            description = type .. value["Current"] .. "%",
            icon = 'fad fa-heartbeat',
            disabled = true
        })
        table.insert(elements, {
            type = 'button',
            name = 'strength',
            label = "Strength",
            description = type .. value["Current"] .. "%",
            icon = 'fad fa-heartbeat',
            disabled = true
        })
        TMC.Functions.OpenMenu({
            namespace = "skillmenu",
            type = 'openMenu',
            title = "Skill menu",
        }, elements, function(close, confirmed)
        end, function(selected)
        end, function(change)
            if change.elementChanged == "stamina" then
                TMC.Functions.CloseMenu()
            elseif change.elementChanged == 'strength' then
                TMC.Functions.CloseMenu()
            end
        end)
    end
end

What it is outputting

What I am looking to achieve:

Stamina stamina25%

Strength strength13%

Thanks in advance

2

There are 2 answers

0
Joseph Sible-Reinstate Monica On

The problem is that you're doing both table.inserts in both iterations, but you should only be creating each menu item once. Either wrap your table.inserts in if statements so they only run for the correct iteration, or just remove the loop altogether and use expressions like Config.Skills.stamina.Current and Config.Skills.strength.Current to get the values.

0
ESkri On

A pairs-loop in Lua means "repeat this action for every item in a table".
So, you should have one table.insert in the loop.
You should also remove OpenMenu invocation from the loop.

local function initcaps(s)
    return s:sub(1,1):upper()..s:sub(2)
end

function SkillMenu()
    FetchSkills()
    Wait(1000)
    RefreshSkills()
    local elements = {}
    for type, value in pairs(Config.Skills) do
        table.insert(elements, {
            type = 'button',
            name = type,
            label = initcaps(type),
            description = type .. " " .. value.Current .. "%",
            icon = 'fad fa-heartbeat',
            disabled = true
        })
    end
    TMC.Functions.OpenMenu({
        namespace = "skillmenu",
        type = 'openMenu',
        title = "Skill menu",
    }, elements, function(close, confirmed)
    end, function(selected)
    end, function(change)
        if change.elementChanged == "stamina" then
            TMC.Functions.CloseMenu()
        elseif change.elementChanged == 'strength' then
            TMC.Functions.CloseMenu()
        end
    end)
end