1
votes

I am trying to edit a script in LUA but I couldn't get access of a local defined in a function

LUA Code

function getSafeMoney()
    local SafeMoney = nil
    
    QBCore.Functions.ExecuteSql(false, 'SELECT * FROM `moneysafes` WHERE `safe` = "mechanic"', function(result)
        SafeMoney = json.decode(json.encode(result[1])).money;
    end)
    return SafeMoney
end

print(getSafeMoney())

Result : nil

here's the sql function as sysdevs asked

QBCore.Functions.ExecuteSql = function(wait, query, cb)
    local rtndata = {}
    local waiting = true
    exports['ghmattimysql']:execute(query, {}, function(data)
        if cb ~= nil and wait == false then
            cb(data)
        end
        rtndata = data
        waiting = false
    end)
    if wait then
        while waiting do
            Citizen.Wait(5)
        end
        if cb ~= nil and wait == true then
            cb(rtndata)
        end
    end
    return rtndata
end
1
could you provide QBCore.Functions.ExecuteSql ? - sysdevs
@sysdevs updated - TiT0

1 Answers

1
votes

Your second function is not holding the main function I assume, I am bad at lua aswell, but I think that changing

QBCore.Functions.ExecuteSql(false, ............

to

QBCore.Functions.ExecuteSql(true, ............

This will probably fix your problem, still your code is ambiguous and needs concentration if you could provide more information I might be able to help more