Atton Posted March 24, 2014 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? Nikola Tesla is love Nikola Tesla is light. Email: [email protected]
Moderators IIYAMA Posted March 24, 2014 Moderators 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) Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
Atton Posted March 24, 2014 Author 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. Nikola Tesla is love Nikola Tesla is light. Email: [email protected]
Anubhav Posted March 24, 2014 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. See my some resources: Skin shop: https://community.multitheftauto.com/in ... ls&id=8008 Note script: https://community.multitheftauto.com/in ... ls&id=8009 Rules Panel: https://community.multitheftauto.com/in ... ls&id=8246 Random Money: https://community.multitheftauto.com/in ... ls&id=8718
Moderators IIYAMA Posted March 24, 2014 Moderators 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. Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
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