-2

I have a problem with my lua script for an MTA Server. When I run the code, I get the following error:

attempt to index field '?' (a nil value)

Here's the code:

addEvent("bank:transfer", true)
addEventHandler("bank:transfer", root, function(id, amount, to, reason)
    if(transferBank(id, amount, to, reason)) then
         triggerClientEvent(client, "bank:transferRecieve", client, true)
    else
         triggerClientEvent(client, "bank:transferRecieve", client, false)
    end
end)

function transferBank(id, amount, to, reason)
    if(id and amount and to and reason) then
        if(BANK_ACCOUNTS[to]) then
            if(BANK_ACCOUNTS[id].balance >= amount) then
                dbExec(connection, "INSERT INTO bank_records (bank_id, record_type, record_from, reason, amount, date) VALUES(?, ?, ?, ?, ?, NOW())", to, 3, id, reason, amount)
                dbExec(connection, "UPDATE bank_accounts SET balance = balance - ? WHERE id=?", amount, id)
                dbExec(connection, "UPDATE bank_accounts SET balance = balance + ? WHERE id=?", amount, to)
                BANK_ACCOUNTS[to].balance = BANK_ACCOUNTS[to].balance + amount
                BANK_ACCOUNTS[id].balance = BANK_ACCOUNTS[id].balance - amount

                return true
            else
                return false, "Le Compte Bancaire spécifié ne contient pas assez d'argent."
            end
        else
            return false, "Le Compte Bancaire spécifié n'existe pas."
        end
    else
        return false, "Argument Invalide."
    end
end

I've been searching for hours, but I can not find where the mistake comes from.

fuenfundachtzig
  • 7,952
  • 13
  • 62
  • 87
StreetOf
  • 1
  • 1
  • Not terribly familiar with lua, but it seems to me like you're expecting to be able to replace `?` characters, which I'm unsure is a legal substitution (see the [lua-users string tutorial](http://lua-users.org/wiki/StringLibraryTutorial)). Your error message seems to line up with that. – Ironcache Apr 25 '18 at 11:13
  • @Ironcache no that's not the problem ;) – Piglet Apr 25 '18 at 11:14
  • @Piglet The lack of `string.format(...)` would also be a problem. But can `?` be subbed in in the manner shown here? – Ironcache Apr 25 '18 at 11:18
  • As an aside, while @Piglet's answer will likely help solve your direct problem, by the documentation, it seems the mentioned string formatting **is** a problem, and will likely need to be resolved before you will get results you desire. – Ironcache Apr 25 '18 at 12:01
  • @Ironcache I don't see any string formatting here. what are you talking about? there are only plain strings in the provided code. – Piglet Apr 25 '18 at 18:25
  • On further inspection, [dbExec documentation](https://wiki.multitheftauto.com/wiki/DbExec) clearly states it works as provided, so my above comment is not correct (did I mention I'm not familiar with Lua?). I thought the author was expecting `?` substitutions to work on strings in general, not realizing that dbExec was providing the functionality. – Ironcache Apr 25 '18 at 22:23

1 Answers1

1

Minimum example that causes this error message:

local BANK_ACCOUNTS = {}
BANK_ACCOUNTS[to].balance = 1

In this case BANK_ACCOUNTS[to] is nil, hence you cannot indedx it. As to is also nil Lua has no field name / key to put into the error message.

BANK_ACCOUNTS["test"].balance = 1

would give you the following eror message:

attempt to index field 'test' (a nil value)

So either to or id is nil at some point.

Because you wouldn't enter if BANK_ACCOUNTS[to] then if to were nil it must be id

Piglet
  • 27,501
  • 3
  • 20
  • 43