Atton Posted March 24, 2014 Share Posted March 24, 2014 I have to ask about something I have been working on say I where to spawn a car using this command. function createCar () car = createVehicle(411,0,0,14) outputChatBox("TRUEm",thePlayer) end addCommandHandler("make",createCar) How could I destroy every car made with this command? Link to comment
Moderators IIYAMA Posted March 24, 2014 Moderators Share Posted March 24, 2014 local myVehicleTable = {} function createCar () local car = createVehicle(411,0,0,14) outputChatBox("TRUEm",thePlayer) myVehicleTable[#myVehicleTable+1]=car-- save inside a table end addCommandHandler("make",createCar) function destroyAllVehicles () for i=1,#myVehicleTable do local vehicle = myVehicleTable[i] if isElement(vehicle) then destroyElement(vehicle) end end myVehicleTable = {}-- reset end addCommandHandler("destroy",destroyAllVehicles) Link to comment
Atton Posted March 24, 2014 Author Share Posted March 24, 2014 local myVehicleTable = {} function createCar () local car = createVehicle(411,0,0,14) outputChatBox("TRUEm",thePlayer) myVehicleTable[#myVehicleTable+1]=car-- save inside a table end addCommandHandler("make",createCar) function destroyAllVehicles () for i=1,#myVehicleTable do local vehicle = myVehicleTable[i] if isElement(vehicle) then destroyElement(vehicle) end end myVehicleTable = {}-- reset end addCommandHandler("destroy",destroyAllVehicles) Destroy does not do anything no error messages. Link to comment
Anubhav Posted March 24, 2014 Share Posted March 24, 2014 local cars = { } function createCar () local car = createVehicle(411,0,0,14) outputChatBox("TRUEm",thePlayer) table.insert(cars,car,car) end function destroyit() for k,v in ipairs(cars) do if isElement(v) then destroyElement(v) cars = { } end end end addCommandHandler("make",createCar) addCommandHandler("destroy",destroyIt) Sorry IIYAMA if you think i copied from your code. Not sure if it works. Link to comment
Moderators IIYAMA Posted March 24, 2014 Moderators Share Posted March 24, 2014 @Atton local myVehicleTable = {} function createCar (thePlayer) local car = createVehicle(411,0,0,14) outputChatBox("TRUEm",thePlayer) myVehicleTable[#myVehicleTable + 1]=car-- save inside a table end addCommandHandler("make",createCar) function destroyAllVehicles (thePlayer) outputChatBox("remove",thePlayer) for i=1,#myVehicleTable do local vehicle = myVehicleTable[i] if isElement(vehicle) then destroyElement(vehicle) end end myVehicleTable = {}-- reset end addCommandHandler("remove",destroyAllVehicles) I and you made a mistake with not defining the player. But it should also work with it defining it wrong, not sure why it didn't work. Try this again, it works for 100% /remove @Anubhav Why don't you fix my code instead of rewrite it? What is the use of that anyway... You made a mistake with your table.insert so your code will no work. and you even took my/Atton mistakes with it. 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