Salem Posted December 2, 2017 Share Posted December 2, 2017 (edited) Hi. I have some issue and can't really deal with it. I have a table: declaration, initialization... local taxidf = { { } }; taxidf[player] = { player = getPlayerName(player), ped = {}, blip = {}, mark = {}, endblip = {}, endmark = {} }; Then to this table I put some values (one of them is below): local ped = createPed( tonumber( id ), DTM_PTS[startPoint][2], DTM_PTS[startPoint][3], DTM_PTS[startPoint][4] + 0.5 ) table.insert( taxidf[player].ped, { ped } ) No matter what's in these DMTs - I need to sketch the problem... Ped is created indeed. And when I want to receive the data later: I get local ped = taxidf[player].ped; I got a message the table length is 1 but table is nil. So I have a question: how should I take the object from this table? What am I doing wrong? To visualize the output I : outputDebugString("ped : "..tostring(ped)..", len: "..tostring(#ped)..", "..tostring(ped[0])..", "..tostring(ped[1]) ); results: `ped: table : 0af4gf3t len : 0, nil, nil` So ped[0] is null, ped is a table.... what to deal with it?? I just want to have back the object in the exact way I do this. Please some suggestions. My target is to: setTimer( killPed, 60, 1, ped ) --- error: [expected element at arg.1, got table] or setTimer( killPed, 60, 1, ped[1] ) --- error: [expected element at arg.1, got nil] you understand... I can't kill this ped! Edited December 2, 2017 by Salem more xplanations Link to comment
Bonus Posted December 2, 2017 Share Posted December 2, 2017 1. Indexing starts at 1 in Lua, not 0. 2. Can you post your full code? ped[1] should be a table, not nil. Link to comment
Salem Posted December 2, 2017 Author Share Posted December 2, 2017 Thanks for reply. It's no point to paste 400+ lines of code, I guess I was clear with this snippets. As you say, ped[1] should be something - I got null. If you can help me please reproduce this and say have you got back the element. Link to comment
Administrators Lpsd Posted December 2, 2017 Administrators Share Posted December 2, 2017 (edited) (edited answer) You are inserting the ped by doing table.insert( taxidf[player].ped, { ped } ) which makes a table inside the ped table. To retrieve the ped, you'd need to do: local ped = taxidf[player].ped[1][1]; But really, unless you need it for a specific reason, you should just change the insert line to: table.insert( taxidf[player].ped, ped ) Edited December 2, 2017 by LopSided_ Link to comment
Salem Posted December 2, 2017 Author Share Posted December 2, 2017 (edited) Haha, You haven't read carefully Lopsided_ - ped[0] is null. But I've found partially the solution: how to kill the ped ped = createPed( tonumber( id ), x, y, z) table.insert( taxidf[player].ped, { ped } ) x = taxidf[player].ped; setTimer( killPed, 6000, 1, x[1][1] ) Couldn't believe ped[1][1] was the solution. So it works if its in one place. Not, if it's in other functions. It didn't help why I had null in further stage of my script but it's out of scope here, I mean it's not connected with tables themselves. Edited December 2, 2017 by Salem I was wrong Link to comment
Moderators IIYAMA Posted December 2, 2017 Moderators Share Posted December 2, 2017 Why do you want to double index, when you are only save one ped at the time? With other words, 2x indexing for nothing. If you want to save multiple peds, then you still have to index 1x for nothing. Link to comment
Salem Posted December 2, 2017 Author Share Posted December 2, 2017 I don't understand why ped[1][1] works if it's in one place (like in code above) but doesn't when calling from other function (nor ped[1] doesn't). the table is global variable in server-side script and I've used such approach earlier in my scripts and it was ok, but here is not. I really don't know what gremlin is in my code. It looks like initializing this table ( taxidf = { { } } ) in one function doesn't save it on global scope. But in all previous scripts it was ok. The only difference here is that I put objects inside not values. Can it be a bug? What kind? Maybe I have some baby mistake? Link to comment
Moderators IIYAMA Posted December 3, 2017 Moderators Share Posted December 3, 2017 --store taxidf[player].ped = ped --load local ped = taxidf[player].ped --validate if isElement(ped) then end No need for more code. Link to comment
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