Jump to content

table problem


Wisin

Recommended Posts

Posted

Hi all, i have a problem with a table, if someone can help please.

addEvent("returnCreatedCheckpoints",true) 
addEventHandler("returnCreatedCheckpoints",getRootElement(), 
function (checkpointsTable) 
checkpoints = {} 
I = 0 
for i,v in pairs(checkpointsTable) do 
I = I +1 
checkpoints[i] = {v[1], v[2], v[3]} 
  end 
end) 
  
addEvent("createNewCheckpoint",true) 
addEventHandler("createNewCheckpoint",getRootElement(), 
function (player,id) 
local x, y, z = checkpoints[tonumber(id)][1], checkpoints[tonumber(id)][2], checkpoints[tonumber(id)][3] -- This outputs: attempt to index field '?' (a nil value) 
triggerClientEvent(player,"createCheckpointsForPlayer",player,x,y,z,id) 
end) 

So my problem is, when do triggerEvent("createNewCheckpoint",player,player,1) it outputs that error.

Posted

what is the point of I = 0 and I = I +1? and where is the checkpointsTable created?

and if your table has ordered indexes, you should use ipairs(), not pairs().

Posted

I = I +1 is to make a total checkpoint count.

And the table is sent from the client side, i dont know if i must use ipairs or pairs.

Posted (edited)

that is fine, the checkpoints gets inserted in the table because i can do:

addEvent("returnCreatedCheckpoints",true) 
addEventHandler("returnCreatedCheckpoints",getRootElement(), 
function (checkpointsTable) 
checkpoints = {} 
I = 0 
for i,v in pairs(checkpointsTable) do 
I = I +1 
checkpoints[i] = {v[1], v[2], v[3]} 
local x, y, z = checkpoints[i][1], checkpoints[i][2], checkpoints[i][3] 
outputDebugString(tostring(x)) 
outputDebugString(tostring(y)) 
outputDebugString(tostring(z)) 
  end 
end) 

and will work fine

Edited by Guest
Posted

how can this work fine if "id" is (probably) nil?

im sorry, but this is confusing. your event "returnCreatedCheckpoints" puts data from some table to another table.

then, event "createNewCheckpoint" doesn't add any new data, but takes some from the table created by "returnCreatedCheckpoints".

and where is "createNewCheckpoint" event is triggered? is "id" passed to it correctly? nvm, found that.

Posted

why dont you do something like:

  
addEvent("createNewCheckpoint",true) 
addEventHandler("createNewCheckpoint",getRootElement(), 
function (player,id) 
  outputDebugString(tostring(id)) -- and see what is there? 
  local x, y, z = checkpoints[tonumber(id)][1], checkpoints[tonumber(id)][2], checkpoints[tonumber(id)][3]  
  triggerClientEvent(player,"createCheckpointsForPlayer",player,x,y,z,id) 
end) 
  

Posted

problem is in this line then:

checkpoints = {v[1], v[2], v[3]}

this syntax doesn't put any values to checkpoints. probably because [ and ] used to denote a numeric index in a table, like {[1] = 1, [2] = "two"}

you can try checkpoints = v, or copy this way

  
local x,y,z = v[1], v[2], v[3] 
checkpoints[i] = { x, y, z } 
  
-- or 
checkpoints[i] = { unpack(v) } 
  

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