Jump to content

Temporary Cars.


GabWas

Recommended Posts

Hi there,

I have a problem with my script for temporary cars. After leaving them, they should be gone and it's working fine, but when someone else create other temporary car, previous one doesn't disappear. I think that I must use tables, but I don't understand them. The tutorials aren't helping me. Here is the code:

function cmd_veh(plr, cmd, ...) 
local vehicleName = table.concat({...}, " ") 
local vehicleID = getVehicleModelFromName(vehicleName) 
local x, y, z = getElementPosition(plr) 
    if isPedInVehicle(plr) then 
        outputChatBox ("#C80000✖  #E7D9B0Get out of the vehicle first.", plr, 255, 255, 255, true) 
    return end 
    if vehicleID then 
        newVehicle = createVehicle (vehicleID, x, y, z, 0, 0, 0) 
        warpPedIntoVehicle(plr, newVehicle) 
        outputChatBox ("#04B404✔  #E7D9B0You created a temporary vehicle named " .. vehicleName .. "#E7D9B0.", plr, 255, 255, 255, true) 
    end 
end 
addCommandHandler("veh", cmd_veh) 
  
function deleteTempVeh(plr, seat, jacked) 
    if not (newVehicle) then return end 
    if (newVehicle) then 
        destroyElement(newVehicle) 
    end 
end 
addEventHandler("onVehicleExit", getRootElement(), deleteTempVeh) 

Link to comment

Yes you need to use a table and use the player element for the index.

-- global table

player_vehicle = {};

-- in the cmd_veh function

if player_vehicle[plr] ~= false then

destroyElement(player_vehicle[plr]);

end

player_vehicle[plr] = createVehicle(...)

-- in onPlayerQuit event handler

if player_vehicle[plr] ~= false then

destroyElement(player_vehicle[plr]);

end

Link to comment

Try:

TEMPORARY_VEHICLE = {}; 
  
function cmd_veh(plr, cmd, ...) 
    local vehicleName = table.concat({...}, " ") 
    local vehicleID = getVehicleModelFromName(vehicleName) 
    local x, y, z = getElementPosition(plr) 
     
    if isPedInVehicle(plr) then 
        outputChatBox ("#C80000✖  #E7D9B0Get out of the vehicle first.", plr, 255, 255, 255, true) 
        return  
    end 
     
    if vehicleID then 
        if TEMPORARY_VEHICLE[plr] ~= false then 
            destroyElement(TEMPORARY_VEHICLE[plr]); 
        end 
         
        TEMPORARY_VEHICLE[plr] = createVehicle (vehicleID, x, y, z, 0, 0, 0) 
        warpPedIntoVehicle(plr, TEMPORARY_VEHICLE[plr]) 
         
        outputChatBox ("#04B404✔  #E7D9B0You created a temporary vehicle named " .. vehicleName .. "#E7D9B0.", plr, 255, 255, 255, true) 
    end 
end 
addCommandHandler("veh", cmd_veh) 
  
function deleteTempVeh(plr, seat, jacked) 
    if not TEMPORARY_VEHICLE[plr] then  
        return  
    end 
     
    destroyElement(TEMPORARY_VEHICLE[plr]) 
end 
addEventHandler("onVehicleExit", getRootElement(), deleteTempVeh) 
  
function PlayerQuit() 
    if not TEMPORARY_VEHICLE[source] then  
        return  
    end 
     
    destroyElement(TEMPORARY_VEHICLE[source]) 
end 
  
addEventHandler"onPlayerQuit", getRootElement(), PlayerQuit); 

Link to comment

Thank you, it's working like a charm!

But one more problem is, that you can create only one car at the time. Is there any way to create multiple cars? For example there is an event and administrator is creating cars for players. Is there any way to do it like that? Because I saw that on some servers.

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