Jump to content

Problem with tables


pejczi

Recommended Posts

Hello,

I've got some problem with tables - i add the serial to the table and when i check if it exists there, it outputs that nope.. What's wrong over here?

serials = { } 
  
  
function addToTable (thePlayer,command) 
local serial = getPlayerSerial(thePlayer) 
    if serial then 
    table.insert(serials,serial) 
outputChatBox ( " #FF0000Serial added successfuly to the table", root, 255, 255, 255, true ) 
end 
end 
addCommandHandler ( "addtable" , addToTable ) 
  
function checkPlayer(thePlayer, command) 
    if serials[serial] then 
    outputChatBox ( " #FF0000There are serials in the table", root, 255, 255, 255, true ) 
        else 
outputChatBox ( " #FF0000There are no serials in the table", root, 255, 255, 255, true ) 
    end 
end 
addCommandHandler ( "checktable" , checkPlayer ) 

Link to comment

table.insert inserts the value into first empty field under integral key starting from 1. That is, if the table is empty, 'table.insert(serials, serial)' will do the same as 'serials[1] = serial', second time it will do the same as 'serials[2] = serial', and so on.

Your checkPlayer function, on the other hand, gets the value from table under the value which is nil, since 'serial' variable is not defined. Which means 'serials[serial]' is nil.

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...