Jump to content

Whole table gets deleted when first element is removed?


Recommended Posts

Hello Community. I have created a table containing all Rhinos and SWAT vehicles.

The script should remove a vehicle from this table if it gets destroyed or streamed out.

Unfortunately if the FIRST tank gets removed by this function, all other elements get removed, too and if i remove the SECOND tank instead of the first, all elements behind the first tank get removed and so on.

I just want to remove the tank that gets streamed out, not this one AND all others behind him. Whats wrong with my script?

Thanks Guys!

tanks = { } 
tankIndex = { } 
  
function table.contains(table, element) 
for _, value in pairs(table) do 
    if value == element then 
        return true 
    end 
end 
return false 
end 
  
function removeFromTable() 
if getElementType( source ) == "vehicle" and (getElementModel(source) == 432 or getElementModel(source) == 601) then 
    if table.contains(tanks, source) then 
        tanks[tankIndex[source]] = nil --this one should only remove the tank that streamed out/got destroyed 
    end 
end 
end 
addEventHandler("onClientElementDestroy", root, removeFromTable) 
addEventHandler( "onClientElementStreamOut", root, removeFromTable) 
  
function tank() 
for key, veh in ipairs ( tanks ) do 
    outputChatBox(tostring(#tanks)) --this returns the number of all Rhinos/SWATs in the table for debugging. 
end 
end 
addEventHandler("onClientRender", root, tank) 

Link to comment

i create this Table.removeValue today try it.

it should work.

function removeFromTable() 
    if getElementType( source ) == "vehicle" and (getElementModel(source) == 432 or getElementModel(source) == 601) then 
        if table.contains(tanks, source) then 
            table.removeValue(tanks, source ) 
        end 
    end 
end 
addEventHandler("onClientElementDestroy", root, removeFromTable) 
addEventHandler( "onClientElementStreamOut", root, removeFromTable) 

Link to comment
Hello Community. I have created a table containing all Rhinos and SWAT vehicles.

The script should remove a vehicle from this table if it gets destroyed or streamed out.

Unfortunately if the FIRST tank gets removed by this function, all other elements get removed, too and if i remove the SECOND tank instead of the first, all elements behind the first tank get removed and so on.

I just want to remove the tank that gets streamed out, not this one AND all others behind him. Whats wrong with my script?

Thanks Guys!

tanks = { } 
tankIndex = { } 
  
function table.contains(table, element) 
for _, value in pairs(table) do 
    if value == element then 
        return true 
    end 
end 
return false 
end 
  
function removeFromTable() 
if getElementType( source ) == "vehicle" and (getElementModel(source) == 432 or getElementModel(source) == 601) then 
    if table.contains(tanks, source) then 
        tanks[tankIndex[source]] = nil --this one should only remove the tank that streamed out/got destroyed 
    end 
end 
end 
addEventHandler("onClientElementDestroy", root, removeFromTable) 
addEventHandler( "onClientElementStreamOut", root, removeFromTable) 
  
function tank() 
for key, veh in ipairs ( tanks ) do 
    outputChatBox(tostring(#tanks)) --this returns the number of all Rhinos/SWATs in the table for debugging. 
end 
end 
addEventHandler("onClientRender", root, tank) 

They aren't removed. However, ipairs can only iterate over an indexed table. If you want to keep the table indexed, use table.remove instead. Otherwise, use pairs to iterate over the table. Both will have a (minimal) performance penalty - the former will be slower on remove, since it shifts all elements to the left, and the latter every time you iterate, because iterating over an unordered table is always slower.

Link to comment

Thank you Walid, I try this asap!

Ixjf, You mean, if I remove element 2 from a table and there are only elements 1, 3 and 4 left, ipairs will do it's job only with element 1 and stop, because there is no element 2? Using pairs will jump over 2 and continue with 3 and 4?

I didn't think about that.

Also, onClientElementStreamOut triggers together with onClientElementDestroy. That's just another bug and I have to remove onClientElementDestroy.

Thx a lot, I will report back tomorrow if it works!

Link to comment

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