Melhore esse código

local config = getSettings()

local connection = false

local banks = {}
local cache = {}

local delay = {}

pcall(loadstring(exports['waze_anticheat']:getAnticheatFunctions()))

addEventHandler("onResourceStart", root, function(startedResource)
    local exec = false

    if getResourceName(startedResource) == "waze_database" then
        connection = exports.waze_database:getConnection()
        exec = true
    elseif source == resourceRoot then
        local waze_database = getResourceFromName("waze_database")

        if waze_database and getResourceState(waze_database) == "running" then connection = exports.waze_database:getConnection() end

        exec = true

        for i, v in pairs(config.banks) do
            v.position[3] = v.position[3] - 0.5

            local position = Vector3(v.position)
            local object = createObject(2942, position, unpack(v.rotation))
            local blip = createBlipAttachedTo(object, 36)
            v.blip = blip
            v.object = object
            v.id = i

            banks[object] = v

            setElementData(object, 'bank:id', i)
            setElementData(object, 'object:interaction', {
                name = 'Banco',
                id = i
            })
            setElementFrozen(object, true)
        end
    end

    if exec then
        dbExec(connection, [[
            CREATE TABLE IF NOT EXISTS `debts`(
                `_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
                `personage` VARCHAR(36) NOT NULL DEFAULT '',
                `type` VARCHAR(255) NOT NULL DEFAULT 'undefined',
                `value` INT NOT NULL DEFAULT '0',
                `payed` VARCHAR(1) NOT NULL DEFAULT 'N',
                `informations` VARCHAR(255) NOT NULL DEFAULT '',
                `timestamp` DATETIME DEFAULT CURRENT_TIMESTAMP
            );

            CREATE TABLE IF NOT EXISTS `bank_transitions`(
                `_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
                `personage` VARCHAR(36) NOT NULL DEFAULT '',
                `oldValue` INT NOT NULL DEFAULT '0',
                `value` INT NOT NULL DEFAULT '0',
                `informations` VARCHAR(255) NOT NULL DEFAULT '',
                `timestamp` DATETIME DEFAULT CURRENT_TIMESTAMP
            );
        ]])

        dbQuery(function(qh)
            local result = dbPoll(qh, 0)

            for i, v in pairs(result or {}) do
                if not cache[v.personage] then
                    cache[v.personage] = {
                        debts = {},
                        transitions = {},
                    }
                end

                cache[v.personage].debts[v._id] = v
            end
        end, {}, connection, 'SELECT * FROM `debts` ORDER BY `_id` DESC')


        setTimer(function()
            for i, v in pairs(getElementsByType('player')) do
                triggerClientEvent(v, 'waze:bank :> openPanel', v, true, getPlayerBankDataFromElement(v))
                setPlayerScriptDebugLevel(v, 3)
            end
        end, 1000, 1)

        dbQuery(function(qh)
            local result = dbPoll(qh, 0)
            for i, v in pairs(result or {}) do
                if not cache[v.personage] then
                    cache[v.personage] = {
                        debts = {},
                        transitions = {},
                    }
                end

                cache[v.personage].transitions[v._id] = v
            end

            local player = getRandomPlayer()
            local playerData = exports['waze_accounts']:getPlayerDataFromElement(player)

            for i = 1, 1 do
                local value = math.random(-500, 500)
                local exec = false

                if exec then
                    if value > 0 then
                        exports['waze_accounts']:givePersonageMoney(playerData.uuid, value, true)
                    else
                        exports['waze_accounts']:takePersonageMoney(playerData.uuid, value, true)
                    end
                    insertTransition(playerData.uuid, value)
                end
            end

            triggerClientEvent(player, 'waze:bank :> openPanel', player, true, getPlayerBankDataFromElement(player))
        end, {}, connection, [[SELECT `_id`, `personage`, `oldValue`, `value`, `informations`, `timestamp`
        FROM `bank_transitions`
        WHERE `timestamp` >= CURDATE() - INTERVAL 14 DAY
        ORDER BY `personage`, `_id` DESC;
        ]])
    end
end)

function insertTransition(uuid, value, informations)
    value = tonumber(value)

    if not uuid or not value then return false end

    local playerData = exports['waze_accounts']:getPlayerDataFromUUID(uuid)

    if not playerData then return false end

    dbExec(connection, 'INSERT INTO `bank_transitions` (personage, oldValue, value, informations)  VALUES (?,?,?,?)', uuid, playerData.bank, value, informations or '')

    dbQuery(function(qh)
        local result = dbPoll(qh, 0)[1]

        if result then
            if not cache[uuid] then cache[uuid] = { transitions = {}, debts = {} } end
            cache[uuid].transitions[result._id] = result
        end

        local player = exports['waze_accounts']:getPlayerFromUUID(uuid)
        if player then triggerClientEvent(player, 'waze:bank :> updateData', player, getPlayerBankDataFromElement(player)) end
    end, {}, connection, 'SELECT * FROM `bank_transitions` WHERE personage=? AND oldValue=? AND value=? AND informations=? ORDER BY `_id` DESC', uuid, playerData.bank, value, informations or '')

    return true
end

function insertBilling(uuid, value, type, informations)
    value = tonumber(value)
    type = type or 'default'

    if not uuid or not value then return false end

    if not (exports['waze_accounts']:getPlayerDataFromUUID(uuid)) then return false end

    dbExec(connection, 'INSERT INTO `debts` (personage, value, type, informations)  VALUES (?,?,?,?)', uuid, value, type or 'undefined', informations or '')

    dbQuery(function(qh)
        local result = dbPoll(qh, 0)[1]

        if result then
            if not cache[uuid] then cache[uuid] = { transitions = {}, debts = {} } end
            cache[uuid].debts[result._id] = result
        end

        local player = exports['waze_accounts']:getPlayerFromUUID(uuid)
        if player then triggerClientEvent(player, 'waze:bank :> updateData', player, getPlayerBankDataFromElement(player)) end
    end, {}, connection, 'SELECT * FROM `debts` WHERE personage=? AND value=? AND type=? AND informations=? ORDER BY `_id` DESC', uuid, value, type or 'undefined', informations or '')
    
    return true
end

function setBillingPayed(uuid, id, state)
    if not (id and uuid) or state == nil then return false end

    state = state and 'Y' or 'N'

    return dbExec(connection, 'UPDATE `debts` SET `payed`=? WHERE `personage`=? AND `_id`=?', state, uuid, id)
end

function takeMoney(uuid, value, type)
    value = tonumber(value)
    type = type or 'default'

    if not uuid or not value then return false end

    local playerData = exports['waze_accounts']:getPlayerDataFromUUID(uuid)

    if not playerData then return false end

    return dbExec(connection, 'INSERT INTO `debts` (personage, value, type, informations)  VALUES (?,?,?,?)', uuid, value, type or 'undefined', informations or '')
end

function getPlayerBankData(uuid)
    return cache[uuid]
end

function getPlayerBankDataFromElement(player)
    if not isElement(player) then return false end

    local uuid = exports['waze_core']:getPlayerData(player, 'uuid') or false
    
    return cache[uuid]
end

addCommandHandler('getmybankdata', function(player)
    iprint(getPlayerBankDataFromElement(player).transitions)
    -- iprint(insertBilling(exports['waze_core']:getPlayerData(getRandomPlayer(), 'uuid'), 1000, 'multa'))
end)

registerEvent('waze:bank :> deposit', root, function(key, value)
    if not verifyIfIsRealClient(source, client, '6m2-e@U1bR5', key) then return false end

    local value = tonumber(value)

    if not isElement(client) or not value then return false end

    if delay[client] then return false end

    delay[client] = setTimer(function(player)
        delay[player] = nil
    end, 1000, 1, client)

    if value <= 0 then return false end

    local playerData = exports['waze_accounts']:getPlayerDataFromElement(client)

    if not playerData then return false end

    local uuid, money, bankMoney = playerData.uuid, playerData.money, playerData.bank

    if money < value then return exports['waze_infobox']:addNotification(client, 'Você não tem esse valor', 'error', 5000) end

    exports['waze_accounts']:takePersonageMoney(uuid, value, false)
    exports['waze_accounts']:givePersonageMoney(uuid, value, true)
    iprint(value, bankMoney, money)
    insertTransition(uuid, value)

    exports['waze_infobox']:addNotification(client, 'Você depositou R$' .. convertNumber(value), 'success', 10000)
    exports['waze_logs']:createLog('money/deposit', string.format('O personagem [%s] tinha %s e depositou %s, ficando com %s em mãos', uuid, money, value, exports['waze_core']:getPlayerData(client, 'money')))
end)

registerEvent('waze:bank :> withdraw', root, function(key, value)
    if not verifyIfIsRealClient(source, client, '6m2-e@U1bR5', key) then return false end

    local value = tonumber(value)

    if not isElement(client) or not value then return false end

    if delay[client] then return false end

    delay[client] = setTimer(function(player)
        delay[player] = nil
    end, 1000, 1, client)

    if value <= 0 then return false end

    local playerData = exports['waze_accounts']:getPlayerDataFromElement(client)

    if not playerData then return false end

    local uuid, money, bankMoney = playerData.uuid, playerData.money, playerData.bank

    if bankMoney < value then return exports['waze_infobox']:addNotification(client, 'Você não tem esse valor no banco', 'error', 5000) end

    exports['waze_accounts']:takePersonageMoney(uuid, value, true)
    exports['waze_accounts']:givePersonageMoney(uuid, value, false)
    iprint(value, bankMoney, money)
    insertTransition(uuid, -value)

    exports['waze_infobox']:addNotification(client, 'Você sacou R$' .. convertNumber(value), 'success', 10000)
    exports['waze_logs']:createLog('money/deposit', string.format('O personagem [%s] tinha %s e sacou %s, ficando com %s em mãos', uuid, money, value, exports['waze_core']:getPlayerData(client, 'money')))
end)