Jump to content

deleting something from table


John Smith

Recommended Posts

Posted (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 by Guest
Posted (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 by Guest
Posted

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

Posted

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 ) 

Posted (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 by Guest
Posted

Because they store it this way:

randomTable = { } 
randomTable [ "my loling text" ] = "myData" 

Then you can do:

randomTable [ "my loling text" ] = nil 

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

Posted

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

Posted

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

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