Einheit-101 Posted June 23, 2015 Share Posted June 23, 2015 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
Walid Posted June 23, 2015 Share Posted June 23, 2015 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
ixjf Posted June 23, 2015 Share Posted June 23, 2015 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
Einheit-101 Posted June 23, 2015 Author Share Posted June 23, 2015 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
ixjf Posted June 23, 2015 Share Posted June 23, 2015 Yes, that's exactly what I meant. Link to comment
Einheit-101 Posted June 24, 2015 Author Share Posted June 24, 2015 Well there appears the following error: ERROR: tanks\tankclient.lua:20: attempt to call field 'remove' (a nil value) and line 20 is this: table.remove(table, index) Does MTA have a problem with table.remove?? Link to comment
ixjf Posted June 24, 2015 Share Posted June 24, 2015 Is 'table' a variable? If so, there's your problem. You've just overwritten the Lua table. Link to comment
Einheit-101 Posted June 24, 2015 Author Share Posted June 24, 2015 Well it suddenly works^^ I changed "table" to "thetable" as parameter, i dont know if that was the problem... It seems to work perfect now. Thanks Link to comment
ixjf Posted June 24, 2015 Share Posted June 24, 2015 Yes, that was obviously the problem 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