Perfect Posted May 9, 2014 Posted May 9, 2014 Hello, I was trying to make players can only spawn 5 vehicles and If they spawned the 6th one. Then the 1st will get destroy and The Table will reset. Here is my Script:- vehicles = { } function spawnVehicle ( myPlayer ) if ( not vehicles[myPlayer] ) then vehicles[myPlayer] = {} end local x,y,z = getElementPosition ( myPlayer ) local car = createVehicle ( 560, x, y, z ) table.insert(vehicles[myPlayer],car) for k,v in pairs(vehicles[myPlayer] do if (#vehicles[myPlayer] >= 5) then destroyElement(vehicles[myPlayer][1]) table.sort[k] else warpPedIntoVehicle ( myPlayer, car ) outputChatBox("Vehicle Spawned",myPlayer, 0, 255, 0, true) setVehicleLocked ( car, true ) end end end addCommandHandler ( "vehCar", spawnVehicle ) Can You Guys Please Help Me ?
tosfera Posted May 9, 2014 Posted May 9, 2014 Why do you want to sort it? An easy way to do this is to use table.remove, it allows you to give an index and it'll shove up all the remaining elements. "table.remove removes an item from an array, shifting down any remaining integer keys" source: http://lua-users.org/wiki/TablesTutorial
Perfect Posted May 9, 2014 Author Posted May 9, 2014 (edited) Oh, I didn't know that there is something like table.remove exist. I just have to put table.remove(vehicles[myPlayer][1]) in replace of table.sort ? And Every other thing is fine ? Edited May 9, 2014 by Guest
TAPL Posted May 9, 2014 Posted May 9, 2014 vehicles = {} function spawnVehiclee(myPlayer) if (not vehicles[myPlayer]) then vehicles[myPlayer] = {} end local x,y,z = getElementPosition(myPlayer) local car = createVehicle(560, x, y, z) table.insert(vehicles[myPlayer], car) if (#vehicles[myPlayer] > 5) then destroyElement(vehicles[myPlayer][1]) table.remove(vehicles[myPlayer], 1) end warpPedIntoVehicle(myPlayer, car) outputChatBox("Vehicle Spawned", myPlayer, 0, 255, 0, true) setVehicleLocked(car, true) end addCommandHandler("vehCar", spawnVehiclee)
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