Jump to content

Removing unused vehicles


Douks08fr

Recommended Posts

Hey everybody, I've got a zombie MTA server running with Shop script (https://community.multitheftauto.com/index.php?p= ... ils&id=720), this script allow players to buy cars/weapons/skills.

With ~80-100 players per day, if they all use this script and buy a car, it will be a very big mess ... :lol:

So, I need your help about destroyElement. I would like to remove the cars who players bought when they die/leave the serv/buy another one. But I don't find how to choose a car created with this script... :roll:

Thanks a lot ;)

Link to comment

edit this script to add a vehicle owner/creator to a vehicle using setElementData().

and onPlayerQuit/onPlayerWasted/buying another vehicle check vehicles and destroy the ones created by player.

or add a vehicle table and put vehicle elements there using player's name as a key, for example.

Link to comment
edit this script to add a vehicle owner/creator to a vehicle using setElementData().

and onPlayerQuit/onPlayerWasted/buying another vehicle check vehicles and destroy the ones created by player.

or add a vehicle table and put vehicle elements there using player's name as a key, for example.

Thanks for your help :)

About the function who will destroy the car :

destroyElement(vehicle) 

How can I retrieve the vehicle to destroy ? If I use "getElementData ( source, "veh" )", what should I store in "veh" key and what should I use with destroyElmnt ?

Thanks

Link to comment
local vehicles = getElementsByType("vehicle") 
for i,v in ipairs (vehicles) do 
 --Anything you want here, for each vehicle (it must be "v") 
end 

Thanks.

So, could this one work :

addCommandHandler ("destroy", 
    function(thePlayer,command) 
    local vehicles = getElementsByType("vehicle") 
        for i,v in ipairs (vehicles) do 
            destroyElement(v) 
        end 
    end 
end) 

?

Link to comment
  • Discord Moderators

That example deletes all cars on the server, prolly now what you want

How can I retrieve the vehicle to destroy ? If I use "getElementData ( source, "veh" )", what should I store in "veh" key and what should I use with destroyElmnt ?

You need to store the vehicle element

function deletePlayerCar (player) 
    local player = player or source 
    local veh = getElementData (player, 'veh') -- get the vehicle element for the player stored under 'veh' 
        if (getElementType (veh) == 'vehicle') then -- if it's a vehicle 
            destroyElement (veh) -- destroy it 
        end 
end 
  
addEventHandler ('onPlayerQuit', root, deletePlayerCar) -- when he leaves 
-- ... 

so when player buys a new car: call function deletePlayerCar before he buys new one

Link to comment
  • Discord Moderators

I bet you're right

use a dynamic array instead

local playerData = {} 
  
function getPlayerVehicle (player) 
    if playerData[player] then 
        return playerData[player].vehicle -- maybe you can just return this, but dunno if it triggers error 
    else 
        return nil 
    end 
end 
  
function createPlayerData (player) 
    if playerData[player] then 
        return 
    end 
    playerData[player] = {} 
end 
  
function destroyVehicle (player) 
    if playerData[player] then 
        if playerData[player].vehicle then 
            destroyElement (playerData[player].vehicle) 
            playerData[player].vehicle = nil 
        else 
            return false 
        end 
    end 
end 

where player gets his vehicle, do this:

destroyVehicle (player) 
playerData[player].vehicle = createVehicle (...) 

and call createPlayerData when he enters shop or similar

Link to comment

Wow, thats harder than i thought ... :lol:

I've made something wrong but ... what. The cars are spawned but the older isn't destroyed.

local playerData = {} 
  
function getPlayerVehicle (player) 
    if playerData[player] then 
        return playerData[player].vehicle -- maybe you can just return this, but dunno if it triggers error 
    else 
        return nil 
    end 
end 
  
function createPlayerData (player) 
    if playerData[player] then 
        return 
    end 
    playerData[player] = {} 
end 
  
function destroyVehicle (player) 
    if playerData[player] then 
        if playerData[player].vehicle then 
            destroyElement (playerData[player].vehicle) 
            playerData[player].vehicle = nil 
        else 
            return false 
        end 
    end 
end 
  
addEvent ("carBuy", true) 
addEventHandler ("carBuy", getRootElement(),  
  
  
function(id2, cost2, name2,x, y, z, sRz, thePlayer) 
  if (getPlayerMoney (source) >= tonumber(cost2)) then 
    outputChatBox ("Achat de " .. name2, source, 255, 0, 0, false) 
    outputChatBox ("Prix: " .. cost2, source, 255, 0, 0, false) 
    takePlayerMoney (source, tonumber (cost2)) 
    local x,y,z = getElementPosition (source) 
    local vehicle = createVehicle (id2, x, y, z, 0, 0, sRz ) 
    destroyVehicle (player) 
    playerData[thePlayer].vehicle = createVehicle (vehicle) 
    warpPedIntoVehicle(source, vehicle) 
    local blii = createBlipAttachedTo ( vehicle, 56, source ) 
    setElementVisibleTo ( blii, getRootElement ( ), false ) 
    setElementVisibleTo ( blii, source, true ) 
  else 
    outputChatBox ("Vous ne pouvez acheter ce produit [pas assez d'argent]", source, 255, 0, 0, false) 
  end 
end) 

Link to comment

Check BLACK

local playerData = {} 
  
function getPlayerVehicle (player) 
    if playerData[player] then 
        return playerData[player].vehicle -- maybe you can just return this, but dunno if it triggers error 
    else 
        return nil 
    end 
end 
  
function createPlayerData (player) 
    if playerData[player] then 
        return 
    end 
    playerData[player] = {} 
end 
  
function destroyVehicle (player) 
    if playerData[player] then 
        if playerData[player].vehicle then 
            destroyElement (playerData[player].vehicle) 
            playerData[player].vehicle = nil 
        else 
            return false 
        end 
    end 
end 
  
addEvent ("carBuy", true) 
addEventHandler ("carBuy", getRootElement(),  
  
  
function(id2, cost2, name2,x, y, z, sRz, thePlayer) 
  if (getPlayerMoney (source) >= tonumber(cost2)) then 
    outputChatBox ("Achat de " .. name2, source, 255, 0, 0, false) 
    outputChatBox ("Prix: " .. cost2, source, 255, 0, 0, false) 
    takePlayerMoney (source, tonumber (cost2)) 
    local x,y,z = getElementPosition (source) 
    local vehicle = createVehicle (id2, x, y, z, 0, 0, sRz ) 
    [b]destroyVehicle (thePlayer)[/b] --"player" doesn't exist here, I replace it for "thePlayer" 
    playerData[thePlayer].vehicle = createVehicle (vehicle) 
    warpPedIntoVehicle(source, vehicle) 
    local blii = createBlipAttachedTo ( vehicle, 56, source ) 
    setElementVisibleTo ( blii, getRootElement ( ), false ) 
    setElementVisibleTo ( blii, source, true ) 
  else 
    outputChatBox ("Vous ne pouvez acheter ce produit [pas assez d'argent]", source, 255, 0, 0, false) 
  end 
end) 

Link to comment
Wow, thats harder than i thought ... :lol:

I've made something wrong but ... what. The cars are spawned but the older isn't destroyed.

local playerData = {} 
  
function getPlayerVehicle (player) 
    if playerData[player] then 
        return playerData[player].vehicle -- maybe you can just return this, but dunno if it triggers error 
    else 
        return nil 
    end 
end 
  
function createPlayerData (player) 
    if playerData[player] then 
        return 
    end 
    playerData[player] = {} 
end 
  
function destroyVehicle (player) 
    if playerData[player] then 
        if playerData[player].vehicle then 
            destroyElement (playerData[player].vehicle) 
            playerData[player].vehicle = nil 
        else 
            return false 
        end 
    else  
      createPlayerData(player) 
    end 
end 
  
addEvent ("carBuy", true) 
addEventHandler ("carBuy", getRootElement(),  
function(id2, cost2, name2,x, y, z, sRz, thePlayer) 
  if (getPlayerMoney (source) >= tonumber(cost2)) then 
    outputChatBox ("Achat de " .. name2, source, 255, 0, 0, false) 
    outputChatBox ("Prix: " .. cost2, source, 255, 0, 0, false) 
    takePlayerMoney (source, tonumber (cost2)) 
    local x,y,z = getElementPosition (source) 
    -- local vehicle = createVehicle (id2, x, y, z, 0, 0, sRz ) 
    destroyVehicle(source) 
    playerData[source].vehicle = createVehicle (id2, x, y, z, 0, 0, sRz ) 
    warpPedIntoVehicle(source, playerData[source].vehicle) 
    local blii = createBlipAttachedTo ( playerData[source].vehicle, 56, source ) 
    setElementVisibleTo ( blii, getRootElement ( ), false ) 
    setElementVisibleTo ( blii, source, true ) 
  else 
    outputChatBox ("Vous ne pouvez acheter ce produit [pas assez d'argent]", source, 255, 0, 0, false) 
  end 
end) 

PS: thePlayer doesnt exist there too (oops, maybe it is, but then what's the "source" for?).

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