Jump to content

[Problem]Table index is nill


Crosshair

Recommended Posts

Hi. I`m trying to make 2 commands that uses one command and i have some problems.

plants = { } 
  
function plants(thePlayer, cmd, id) 
    if (cmd == "plant") then 
        local x,y,z = getElementPosition(thePlayer) 
        plants[id] = createObject(plantID,x,y,z) 
    end 
    if (cmd == "harvest") then 
        --TODO 
    end 
         
end 
addCommandHandler("plant", plants) 
addCommandHandler("harvest", plants) 

When i enter the command "plant" gives me an error at plants[id] = createObject(plantID,x,y,z): table index is nill.

If i remove the argument "cmd" from the function all works well.

Link to comment

This is maybe because you just entered the command "plant" and you did not wrote an id (like /plant 29 ).

function plants(thePlayer, cmd, id) 
    if (cmd == "plant") then 
        if tonumber(id) then 
            local x,y,z = getElementPosition(thePlayer) 
            plants[id] = createObject(plantID,x,y,z) 
        end 
    end 
    if (cmd == "harvest") then 
        --TODO 
    end 
        
end 

Link to comment

Your right, but i want the player to not enter a number. Just to type plant and the plant will later rise from the ground, So i need the script to add the plant1 in the table with the first position and if he decides to add another plant(plant2) to be added also in the table with the second position.

If i don`t use the cmd argument this works and if i use outputchatbox(plants[1]) works.

Link to comment
Your right, but i want the player to not enter a number. Just to type plant and the plant will later rise from the ground, So i need the script to add the plant1 in the table with the first position and if he decides to add another plant(plant2) to be added also in the table with the second position.

If i don`t use the cmd argument this works and if i use outputchatbox(plants[1]) works.

Then dont use id as table index, use this:

table.insert(plants,createObject(plantID,x,y,z)) 

Link to comment
You can't declare different things with the same name.

There is also no "plantID" variable declared.

I just didn`t included the variable.

Then dont use id as table index, use this:

table.insert(plants,createObject(plantID,x,y,z)) 

I make another thing right now.

I added on the top id = 0, removed the id argument and add id=id+1 before it creates the object.

Now if i use table.insert how do i retrieve the emelents ? with unpack ? What is the best option to do this ? Mine or yours ?

Link to comment

Mine is easier, table.insert, will do what you want to do with "id = 0 and id = id+1".

You can retrieve the elements so: plants[1] is the first added element plants[2] is the second and so on, or do a loop:

--this will destroy every elements in the table if it exists, and remove it from the table 
for i, v in ipairs(plants) do 
    if isElement(v) then 
        destroyElement(v) 
    end 
    table.remove(plants,i) 
end 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...