Jump to content

[HELP] Destroy vehicle before create another one


FIY9AL

Recommended Posts

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
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 by holuzs
  • Like 1
Link to comment
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 by Developer.
  • Thanks 1
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...