FIY9AL Posted January 8, 2020 Share Posted January 8, 2020 Hello, i'm new in Lua mta sa scripting, I want to make a code to destroy the previous vehicle (of this script, not any vehicle created by server) in order to create another vehicle. So, i make this code for client side: -- Button1 Function -- (Client Side) addEventHandler ("onClientGUIClick", getRootElement(), function() if (source == button1) then local playerxyz = getLocalPlayer() local selectedList1 = guiGridListGetItemData ( list1, guiGridListGetSelectedItem ( list1 ), 1 ) triggerServerEvent ( "spawncar", getLocalPlayer(), selectedList1,playerxyz) end end ) and this is for the server side: -- Button1 Function -- (Server Side) function serverSpawn(selectedList1,playerxyz) local x, y, z = getElementPosition ( playerxyz ) local rx,ry,rz = getElementRotation(playerxyz) if ( isPedInVehicle (playerxyz) ) then outputChatBox( "[Error] Exit your current vehicle first.", source, 255, 0, 0, true ) else local playerVehicle = createVehicle ( selectedList1, x+1, y+1, z+0.5, rx,ry,rz ) local playerCreatedVehicle = getPedOccupiedVehicle ( playerxyz ) if ( playerCreatedVehicle == playerVehicle ) then destroyElement ( playerVehicle ) else warpPedIntoVehicle ( playerxyz, playerVehicle ) end end end addEvent("spawncar",true) addEventHandler("spawncar",getRootElement(),serverSpawn) So evreytime player create vehicle from gui list (script) the previous vehicle dosen't destroy, i want evreytime the player create a new car from this script, the previous vehicle destroyed. please help... Link to comment
holuzs Posted January 8, 2020 Share Posted January 8, 2020 (edited) local playersVehicles = {}; function serverSpawn(selectedList1,playerxyz) local x, y, z = getElementPosition(playerxyz) local rx,ry,rz = getElementRotation(playerxyz) if (getPedOccupiedVehicle(playerxyz)) then removePedFromVehicle(playerxyz); destroyElement(getPedOccupiedVehicle(playerxyz)); end if (playersVehicles[playerxyz]) then local veh = playersVehicles[playerxyz]; if (isElement(veh)) then destroyElement(veh); end end local playerVehicle = createVehicle ( selectedList1, x+1, y+1, z+0.5, rx,ry,rz ) local playerCreatedVehicle = getPedOccupiedVehicle ( playerxyz ) warpPedIntoVehicle ( playerxyz, playerVehicle ) playersVehicles[playerxyz] = playerVehicle; end addEvent("spawncar",true) addEventHandler("spawncar",getRootElement(),serverSpawn) Edited January 8, 2020 by holuzs 1 Link to comment
Developer. Posted January 8, 2020 Share Posted January 8, 2020 (edited) local tableVehicle = {} function serverSpawn(selectedList1,playerxyz) local x, y, z = getElementPosition ( playerxyz ) local rx,ry,rz = getElementRotation(playerxyz) if ( isPedInVehicle (playerxyz) ) then outputChatBox( "[Error] Exit your current vehicle first.", playerxyz, 255, 0, 0, true ) else if ( tableVehicle[playerxyz] ) then destroyElement ( tableVehicle[playerxyz] ) tableVehicle[playerxyz] = nil end local playerVehicle = createVehicle ( selectedList1, x+1, y+1, z+0.5, rx,ry,rz ) warpPedIntoVehicle ( playerxyz, playerVehicle ) tableVehicle[playerxyz] = playerVehicle end end addEvent("spawncar",true) addEventHandler("spawncar",getRootElement(),serverSpawn) hm, try this... Edited January 8, 2020 by Developer. 1 Link to comment
FIY9AL Posted January 9, 2020 Author Share Posted January 9, 2020 Thank you very much "Developer." i'm really appreciate you. it is working perfectly. and thank you "holuzs" for helping me. 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