Jump to content

Question - Vehicle


manve1

Recommended Posts

Posted

Is it possible to destroy a vehicle with same name but from the same person who spawned it, not from like if one person spawns it, then next one and first vehicle that got spawned gets destroyed?

Posted

You mean when player1 spawns an Infernus, spawns a NRG-500 and spawns another Infernus, the first spawned Infernus is destroyed?

Easy. Just use a table. Script comes down to this:

-- create a table in which we're going to put the vehicle-tables for players 
playerVehicles = {} 
  
-- create a table in which we're going to save the players that spawned the vehicles 
vehiclePlayers = {} 
  
function playerJoin() 
    -- when player joins, create the player's vehicletable 
    playerVehicles[source] = {} 
end 
function playerQuit() 
    -- loop through all the vehicles the player spawned and destroy them 
    for model, vehicle in pairs ( playerVehicles[source] ) do 
        if vehicle and isElement(vehicle) then 
            vehiclePlayers[vehicle] = nil 
            destroyElement ( vehicle ) 
        end 
    end 
  
    -- destroy the player's vehicleable when he leaves 
    playerVehicles[source] = nil         
end 
  
-- when a vehicle is spawned by a player 
function playerSpawnsVehicle(player, vehicleElement) 
    -- get the spawned vehicle's model 
    local vehicleModel = getVehicleModel(vehicleElement) 
  
    -- check if there is a value for the vehiclemodel in the player's vehicletable 
    if playerVehicles[player][vehicleModel] and isElement(playerVehicles[player][vehicleModel]) then 
  
        -- if it exists, destroy that earlier spawned vehicle 
        destroyElement ( playerVehicles[player][vehicleModel] ) 
    end 
     
    -- set the new spawned vehicle as element for the vehiclemodel in the player's vehicletable 
    playerVehicles[player][vehicleModel] = vehicleElement 
  
    -- save who created the vehicle 
    vehiclePlayers[vehicleElement] = player 
end  
  
-- if a vehicle is destroyed, delete it from the table from the player that spawned it 
function vehicleDestroyed() 
    -- get player who spawned the vehicle 
    local player = vehiclePlayers[source] 
  
    -- destroy data in the player's table, if the player still exists 
   if player and isElement(player) then 
        playerVehicles[player][getElementModel(source)] = nil 
    end 
    -- remove this vehicle from the vehiclePlayers table 
    vehiclePlayers[source] = nil 
end 

I didn't add function handlers, the function-names should explain where to put the parts in your script.

Posted

Puma ur script didn't work, even i can create (( 1 person )) tons of vehicles, after all, i mean like:

Two people spawn a vehicle, but when they both re-spawn their vehicles, old ones get destroyed.

And

Two people spawn a vehicle, when one of them re-spawns same id vehicle that was created by 'createVehicle' function, it only destroy's the person who needed re-spawn of vehicle

Posted

No duh it doesn't work on it's own, I expected you to implement it in your script. You only asked for a way how do do it and I gave an example.

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...