Warm tip: This article is reproduced from stackoverflow.com, please click
lua

Not returning correct value

发布于 2020-04-10 16:09:39

Hello I have 2 functions one to return a value

function AddPlayer(steamID, source)
    for i,p in ipairs(players) do
        if steamID == p[1] then
            players[i] = {p[1], p[2], source}
            return
        end
    end

    local initialPoints = GetInitialPoints(steamID)
    print(initialPoints)
    table.insert(players, {steamID, initialPoints, source})
end

function GetInitialPoints(steamID)
    local points = 0
    print(steamID)
    MySQL.Async.fetchAll("Select priority FROM queue where `identifier`= @identifier",
    {
        ['@identifier'] = steamID,
    },
    function(resp)
        points = resp[1].priority
        print(points)
    end)
    return points
end

so the print actually prints the correct value (10,000)

but in the return on function AddPlayer where I print the initial points it is printing 0 which is what I set the variable when declared. When it needs to be printing what I set points to.

Questioner
jake pavek
Viewed
49
Egor Skriptunoff 2020-02-02 05:41
function AddPlayer(steamID, source)
    for i,p in ipairs(players) do
        if steamID == p[1] then
            players[i] = {p[1], p[2], source}
            return
        end
    end
    print(steamID)
    MySQL.Async.fetchAll(
        "Select priority FROM queue where `identifier`= @identifier",
        {
            ['@identifier'] = steamID,
        },
        function(resp)
            local initialPoints = resp[1].priority
            print(initialPoints)
            table.insert(players, {steamID, initialPoints, source})
        end
    )
end