Wisin Posted June 10, 2011 Posted June 10, 2011 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.
Aibo Posted June 10, 2011 Posted June 10, 2011 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().
Wisin Posted June 10, 2011 Author Posted June 10, 2011 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.
Wisin Posted June 10, 2011 Author Posted June 10, 2011 (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 June 10, 2011 by Guest
Aibo Posted June 10, 2011 Posted June 10, 2011 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.
Wisin Posted June 10, 2011 Author Posted June 10, 2011 Sorry, i forgot to change tonumber(id), and yes id is passed, i use triggerEvent to pass it.
Aibo Posted June 10, 2011 Posted June 10, 2011 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)
Wisin Posted June 10, 2011 Author Posted June 10, 2011 because i already did that, and it says what it should say.
Aibo Posted June 10, 2011 Posted June 10, 2011 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) }
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now