Jump to content

Lua table problem


InDev

Recommended Posts

Hello,

I'm here because i have a little problem in my script it's a table problem ( I'm always wrong with the tables :( )

Here is my code:

Init the table:

taxiCalls = {{Element,X,Y,Z}}

Make a new table index for the new call:

-- This is a part of a function who works perfectly just this don't work
local newCallInd = #taxiCalls
while( taxiCalls[newCallInd ]["Element"] ~= nil )do
		newCallInd = newCallInd +1
end	
 

My problem is that when the While is executed there is the following sentence who appears in the console when he don't find the index of the table:

attempt to index field '?' (a nil value) --with a reference to the "While" line number

I don't understand why it works fine the first time i call this function and not the second time...

Thanks for the help.

Link to comment

When you initiate the table like

taxiCalls = {{Element,X,Y,Z}}

it's the same as doing

taxiCalls = {{[1] = Element,[2] = X,[3] = Y,[4] = Z}}

So there is no table key Element. That's why it's says attempt to index field ?

Change

while( taxiCalls[newCallInd ]["Element"] ~= nil )do

to

while( taxiCalls[newCallInd ][1] ~= nil )do

Then you actually call the index [1] which returns the element.

Link to comment

Here is the whole function:

taxiCalls= {{[1]=Element,[2]=X,[3]=Y,[4]=Z}}
function displayServices( thePlayer, commandName, ... )
local textT = {...}
local text = table.concat( textT, " " )
if( text == "taxi" or text == "Taxi" )then
local servicesPlayers = {}
for k,i in ipairs( getElementsByType( "player" ) )do
if( isMember( i, "Taxi" ) )then
if( getElementData( i, "inService" ) == 1 )then
				servicesPlayers[k] = i
outputChatBox( ""..getPlayerName( i ) )
end
end
end
if( #servicesPlayers > 0 )then
local x,y,z = getElementPosition( thePlayer )
local newCallsInd = #taxiCalls
while( taxiCalls[newCallsInd][1] )do
			newCallsInd = newCallsInd+1
end
for k,i in ipairs( servicesPlayers ) do
outputChatBox( "A new call is available ( Caller: "..getPlayerName( thePlayer).."  Number: "..newDemandeInd..". )", i, 255, 255, 255 )
end
outputChatBox( "Your call has been send to all taxi drivers, please wait for an answer.", thePlayer, 0, 175, 0 )
		taxiCalls[newCallsInd] = {}
                       taxiCalls[newCallsInd][1] = thePlayer
		taxiCalls[newCallsInd][2] = X
		taxiCalls[newCallsInd][3] = Y
		taxiCalls[newCallsInd][4] = Z
end
elseif( text == "" )then
 
end
end
addCommandHandler( "services", displayServices )

EDIT: Added the table init.

EDIT2: Added the "taxiCalls[newCallsInd] = {}"

Edited by Guest
Link to comment

I don't see the taxiCalls table being initiated anywhere... Also, I don't see taxiCalls[newCallsInd] being initiated anywhere. You need to have

taxiCalls[newCallsInd] = {}

before

taxiCalls[newCallsInd][1] = thePlayer

Link to comment

Sorry i don't understand the question i'm French and don't have a complete comprehension of what you are saying

I need the " while " because when I use the function with only #taxiCalls as index the second call erase the first in the table and when I use #taxiCalls+1 I can't use the for loop to read the calls and I don't know why too, I'm a noob with the tables ^^

Edited by Guest
Link to comment

"First question:"

"What doesn't work? "Increase the variable to use as a new index for the table with a while

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

Where ? at the 'while' line

"Second question:"

"What is the point of the while loop ?" What do you mean by point ?

Link to comment

Oh I resolved the problem i post the whole function if you want:

function displayServices( thePlayer, commandName, ... )
local textT = {...}
local text = table.concat( textT, " " )
if( text == "taxi" or text == "taxi" )then
local servicesPlayers = {}
for k,i in ipairs( getElementsByType( "player" ) )do
if( isMember( i, "Taxi" ) )then
if( getElementData( i, "inService" ) == 1 )then
				servicesPlayers[k] = i
outputChatBox( ""..getPlayerName( i ) )
end
end
end
if( #servicesPlayers > 0 )then
local x,y,z = getElementPosition( thePlayer )
local newCallsInd = #taxiCalls+1
for k,i in ipairs( servicesPlayers ) do
outputChatBox( "A new call is avalaible ( Caller: "..getPlayerName(thePlayer).."  Number: "..(newCallsInd-1)..".  )", i, 255, 255, 255 )
end
outputChatBox( "Your call has been send to all taxi drivers, please wait for an answer", thePlayer, 0, 175, 0 )
		taxiCalls[newCallsInd] = {}
		taxiCalls[newCallsInd][1] = thePlayer
		taxiCalls[newCallsInd][2] = x
		taxiCalls[newCallsInd][3] = y
		taxiCalls[newCallsInd][4] = z
end
elseif( text == "" )then
 
end
end
addCommandHandler( "services", displayServices )

Thank you for the help, The while loop didn't worked cause when he check for the table index, he didn't find it so the function stop, that's my opinion, I can be wrong.

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