John Smith Posted December 31, 2014 Share Posted December 31, 2014 (edited) hello, if source element is in a table why can't i remove it like this? playersTable[source] = nil the player is still in table somehow edit: could it be because im inserting values with table.insert? Edited December 31, 2014 by Guest Link to comment
Castillo Posted December 31, 2014 Share Posted December 31, 2014 Are you sure that 'source' is the correct element? Link to comment
John Smith Posted December 31, 2014 Author Share Posted December 31, 2014 (edited) yes its an element, i also tried putting strings in the table,everything on runcode as well and it just wouldnt remove it from table(checked by unpack function) here an easy working example randomTable = {} a = "i need to get removed from a table seriously" table.insert(randomTable,a) -- its inserted randomTable[a] = nil -- should remove it but doesn't for me. Edited December 31, 2014 by Guest Link to comment
Castillo Posted December 31, 2014 Share Posted December 31, 2014 Mind posting the rest of your code? Link to comment
John Smith Posted December 31, 2014 Author Share Posted December 31, 2014 i don't have a rest of code, i'm working on a prototype but i have found out about this issue while trying some table things in game, i edited my post above to show method how im manipulating table Link to comment
Castillo Posted December 31, 2014 Share Posted December 31, 2014 Well, now that you posted the way you insert to your table, I can see the problem. That table is an indexed one, that's why you can't remove the items that way, you need to use it like this: randomTable [ 1 ] = nil Or simply using: table.remove ( randomTable, 1 ) Link to comment
John Smith Posted December 31, 2014 Author Share Posted December 31, 2014 (edited) but if i have thousands of things in my table, how can i find out which "ID" is the thing which i want to remove? also how come other scripts can use the above method with source like mine? i saw freeroam lots of times using playerBlips[source] = nil and it worked for them -- well, something like this. Edited December 31, 2014 by Guest Link to comment
Castillo Posted December 31, 2014 Share Posted December 31, 2014 Because they store it this way: randomTable = { } randomTable [ "my loling text" ] = "myData" Then you can do: randomTable [ "my loling text" ] = nil Link to comment
John Smith Posted December 31, 2014 Author Share Posted December 31, 2014 alright thanks. just out of curiosity is there any other way to add/remove things in a table? i mean where it gets looped and stuff Link to comment
Gallardo9944 Posted December 31, 2014 Share Posted December 31, 2014 playersTable[source] = nil ^ works only when you used something like this before: playersTable[source] = true If you have an indexed table, as Solidsnake14 pointed out, you have to remove that only by its position in the table. If you want to communicate with the table by the elements and not by IDs, you could better use the way of playersTable[source] = true. In other cases, you can loop through the table and remove the required position: for i,v in ipairs(playersTable) do if source == v then -- if there is such element table.remove(playersTable,i) -- remove it from the table according to its index end end ^ this way is not really recommended, but works in case you want to know how it is made. Link to comment
John Smith Posted December 31, 2014 Author Share Posted December 31, 2014 alright thank you guys alot for help and i am very thankful for a very fast reply and happy new year to both of you! Link to comment
John Smith Posted January 2, 2015 Author Share Posted January 2, 2015 hello i am a bit confused something = {} something[#something+1] = {playerElement} something[#something+1] = {"text","another text",someVariable} so how would i find an exact thing that i want to delete? how would i know the id of thing to remove? looping through it wouldnt help as in gallardo's example it would need to be v[1][1] for example at line 2 if anyone can help me please do Link to comment
Castillo Posted January 2, 2015 Share Posted January 2, 2015 Why wouldn't his example work? Link to comment
John Smith Posted January 2, 2015 Author Share Posted January 2, 2015 It just didnt work in runcode. I couldnt use if variable == v then table.remove(table,i) end I wouldnt need to use something like this instead to delete something if variable == v[somenumber][anothernumber] then ... 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