[FAQ] "the index _____ does not exist"

Started by Stormeus, Jul 18, 2016, 06:45 PM

Previous topic - Next topic

Stormeus

AN ERROR HAS OCCURRED: the index ______ does not exist



This is probably the most frequently posted error on the forum, and serves as a daily reminder that people really don't search for their issue before posting. In any case, because of the frequency with which this issue is reported, and the various different script issues that can cause it, here is an almost-comprehensive guide on its causes and fixes.

At its core, this error simply means that Squirrel couldn't find a variable or function that you are accessing or setting, such as in the following cases:



Case #1
Example

    local myVariable;
    variable = 5;

Solution
Squirrel will throw an error here because you are trying to assign a value to a variable that does not even exist yet. This can be caused by typos, by recklessly copying and pasting code, or by simply forgetting what names you're using for a certain variable.

In this case, we created the variable myVariable, but then try to set variable to 5. variable doesn't exist, so the solution would be to change the code to this:

    local myVariable;
    myVariable = 5;



Case #2
Example

    local myVariable;
    local myValue = 5;
    myVariable = value;

Solution
For the same reason as case #1, an error occurs here because you try to use the variable value, which doesn't exist. The correct code would look like this:

    local myVariable;
    local myValue = 5;
    myVariable = myValue;



Case #3
Example

    local myVariable = playerInfo();

Solution
In this case, the function or class playerInfo simply does not exist, and needs to be created in order for it to work. There are essentially two causes behind this:
  • Trying to use SQLite or hashing functions without loading the modules in server.cfg
  • Not actually creating the function or variable

If we were trying to use QuerySQL() instead of playerInfo(), we would have to go into server.cfg and add plugins sqlite04<release type>

However, because this is an error caused by a missing class, we would simply create the class before this function:

    class playerInfo
    {
        // omitted for brevity
    }

    local myVariable = playerInfo();

Or, if it were a function:

    function playerInfo()
    {
        // omitted for brevity
    }
    local myVariable = playerInfo();



Case #4
Example

    local plr = FindPlayre(0);
    print( plr.ID );

Solution
FindPlayer is spelled incorrectly:

    local plr = FindPlayer(0);
    print( plr.ID );



Case #5
Example

    local plr = FindPlayer(0);
    print( plr.ID );

Solution
plr is null because no player with ID #0 is actually online. If this is the case in your script, make sure you check that the player exists before trying to do anything with it.

    local plr = FindPlayer(0);
    if( plr ) print( plr.ID );
    else print( "No player with ID #0 is online." );