Jump to content

Destruir coche


vallejo

Recommended Posts

Tengo esta función, porque cuando utilizo el comando no lo hace, y normalmente si destruye el coche si existe.

car = {} 
  
function vehicle(vehicle) 
  
    DestruirVehiculo() -- Tambn le pase el parámetro y no funciona 
  
    if not getPedOccupiedVehicle( source ) then 
        local vehicleID = getVehicleIDFromName(vehicle) 
        local x,y,z = getElementPosition( source ); x = x + 5 
        car[source] = createVehicle( vehicleID, x, y, z ); 
        warpPedIntoVehicle(source, car[source]) 
    end 
        return true; 
end 
addEvent("onCrearVehiculo", true) 
addEventHandler("onCrearVehiculo", root,vehicle) 
  
function DestruirVehiculo() 
    if car[source] then 
        destroyElement(car[source]) 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 

Me gustaría utilizar un setTimer para un determinado tiempo destruir los autos q no son ocupados por player, como lo puedo implementar?

Con la función

logOut  

compruebo si un player está login?, o como hago para que cuando un jugador se desconecte se destruya el coche?

Link to comment

Usa onPlayerLogout para lo del logOut.

Mira, tengo este problema: El coche se crea bien y todo y se destruye bien, cuando uso el comando tambn lo destruye pero cuando quiero crear otro coche me sale un error: bad argument @ 'destroyElement' [expected element at argument 1]

car = {} 
  
function vehicle(vehicle) 
    DestruirVehiculo(source, _) 
  
    if not getPedOccupiedVehicle( source ) then 
        local vehicleID = getVehicleIDFromName(vehicle) 
        local x,y,z = getElementPosition( source ); x=x+1 
         
        car[source] = createVehicle( vehicleID, x, y, z, 0, 0, 180); 
        warpPedIntoVehicle(source, car[source]) 
    end 
        return true; 
end 
addEvent("onCrearVehiculo", true) 
addEventHandler("onCrearVehiculo", root,vehicle) 
  
  
function DestruirVehiculo(source, _) 
    if car[source] then 
        destroyElement(car[source]) 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout",getRootElement(),DestruirVehiculo) 

Link to comment

Usa onPlayerLogout para lo del logOut.

Mira, tengo este problema: El coche se crea bien y todo y se destruye bien, cuando uso el comando tambn lo destruye pero cuando quiero crear otro coche me sale un error: bad argument @ 'destroyElement' [expected element at argument 1]

car = {} 
  
function vehicle(vehicle) 
    DestruirVehiculo(source, _) 
  
    if not getPedOccupiedVehicle( source ) then 
        local vehicleID = getVehicleIDFromName(vehicle) 
        local x,y,z = getElementPosition( source ); x=x+1 
         
        car[source] = createVehicle( vehicleID, x, y, z, 0, 0, 180); 
        warpPedIntoVehicle(source, car[source]) 
    end 
        return true; 
end 
addEvent("onCrearVehiculo", true) 
addEventHandler("onCrearVehiculo", root,vehicle) 
  
  
function DestruirVehiculo(source, _) 
    if car[source] then 
        destroyElement(car[source]) 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout",getRootElement(),DestruirVehiculo) 

     
if car[source] then 
   destroyElement(car[source]) 
   car[source] = nil 
end 

Link to comment

Gracias.

Mira se supone que esto cada que me desconecto debería destruir el coche y porque no lo hace.

function DestruirVehiculo(source, _) 
if car[source] then 
    destroyElement(car[source]) 
    car[source] = nil 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout",source, DestruirVehiculo) 

Link to comment
Gracias.

Mira se supone que esto cada que me desconecto debería destruir el coche y porque no lo hace.

function DestruirVehiculo(source, _) 
if car[source] then 
    destroyElement(car[source]) 
    car[source] = nil 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout",source, DestruirVehiculo) 

No nombres los parámetros de las funciones como "source", es un mal hábito que solo servirá para confundirte.

function DestruirVehiculo(player) 
if eventName then player = source end 
if car[player] then 
    destroyElement(car[player]) 
    car[player] = nil 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout", root, DestruirVehiculo) 

Edited by Guest
Link to comment

Predefined_variables_list

MTA Predefined variables

Shared

exports -- returns a table of resource names containing all export functions 
resource -- returns a resource element of the resource the snippet was executed in 
resourceRoot -- returns a resource root element of the resource the snippet was executed in 
root -- returns the root element of the server 

Client Only

guiRoot -- returns the root element all GUI elements. 
localPlayer -- returns the player element of the local player. 

The list of hidden variables, that can be found in functions - handlers:

Shared

source -- The player or element the event was attached to 
this -- Element, which was attached function-handler. 
eventName -- the name of the event ("onResourceStart", "onPlayerWasted" etc.) 

Server Only

client -- the client that called the event 

local car = {} 
  
function vehicle(vehicle) 
    if client then 
    source = client 
    end 
    if car[source] then 
    destroyElement(car[source]) 
    car[source] = nil 
    end 
    if not getPedOccupiedVehicle( source ) then 
    local vehicleID = getVehicleIDFromName(vehicle) 
    local x,y,z = getElementPosition( source ) 
    car[source] = createVehicle( vehicleID, x+5, y, z ); 
    local x,y,z = nil 
    warpPedIntoVehicle(source, car[source]) 
    end 
end 
addEvent("onCrearVehiculo", true) 
addEventHandler("onCrearVehiculo", root,vehicle) 
  
function DestruirVehiculo(source) 
    if car[source] then 
        destroyElement(car[source]) 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 

Link to comment

No funciona, aun sigue el error en el parámetro 2 de el addEventHandler el source

function DestruirVehiculo(player) 
if eventName then player = source end 
if car[player] then 
    destroyElement(car[player]) 
    car[player] = nil 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout",source, DestruirVehiculo) 

Link to comment
No funciona, aun sigue el error en el parámetro 2 de el addEventHandler el source
function DestruirVehiculo(player) 
if eventName then player = source end 
if car[player] then 
    destroyElement(car[player]) 
    car[player] = nil 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout",source, DestruirVehiculo) 

No puedes usar "source" en un trigger a un evento, donde source aun no existe.

addEventHandler("onPlayerLogout",root, DestruirVehiculo) 

Link to comment

No nada aun sigue sin destruirlo, esto e lo que tengo en server side

marker = {} 
  
function Marcador( element ) 
    if getElementType( element ) == "player" then 
        local veh = getPedOccupiedVehicle( element ) 
        if veh then 
            return  
        else 
            triggerClientEvent(element, "mostrarPanel", element) 
        end 
    end 
end 
  
----------Crear Marcador y comprobamos si es administrador--------------------- 
function crearMarcador(thePlayer, cmd) 
local obtCuent = getAccountName(getPlayerAccount(thePlayer)) -- Obtenemos el nombre de la cuenta 
    if isObjectInACLGroup("user."..obtCuent, aclGetGroup("Admin"))  then -- Comparamos si esta en el grupo de administradores 
        local marcadores = LeerTxt(thePlayer) 
        for i = 1, #marcadores do 
            local x, y, z = unpack(marcadores[i]) --tipo, tamano, r, g, b, a 
            if not marker[i] then 
                marker[i] = createMarker(x, y, z, "cylinder", 2, 0 ,255, 0, 155, root ) 
                createBlip( x, y, z, 51, 0, 0, 0, 255, 0, 0, 300.0, source ) 
                addEventHandler("onMarkerHit", marker[i], Marcador) --Llamamos la funcion Marcador cuando un jugador toca el marker 
            end 
        end 
    end 
end 
  
  
function crearTxt(thePlayer) ------------------- Creamos el archivo y lo escribimos, obtener la posición del marcador ----------------------------------  
  local x, y, z = getElementPosition(thePlayer) -- Obtenemos la posición del jugador 
    local position = tostring(x) .. ", " .. tostring(y) ..", ".. tostring(z-1) .."\r\n" 
  
    if fileExists ( "posicion.txt" ) then 
        local file = fileOpen("posicion.txt") 
        fileSetPos ( file, fileGetSize ( file ) ) 
        fileWrite(file, position) 
        fileClose(file) 
    else 
        local file = fileCreate("posicion.txt") 
        fileWrite(file, position) 
        fileClose(file) 
    end 
    crearMarcador(thePlayer) 
end 
addCommandHandler("crear", crearTxt) 
  
function LeerTxt(Player) 
    local marcadores = {} 
    local hFile = fileOpen("posicion.txt", true) 
    
    if hFile then                                   
        buffer = fileRead(hFile, fileGetSize(hFile))     
        strtable = split(buffer, "\r\n")         
        for i = 1, #strtable do 
            local x = tonumber(gettok(strtable[i], 1, ",")) 
            local y = tonumber(gettok(strtable[i], 2, ",")) 
            local z = tonumber(gettok(strtable[i], 3, ",")) 
            marcadores[i] = {x, y, z} 
        end     
        fileClose(hFile) 
        return marcadores 
    else 
        outputConsole("Unable to open test.txt") 
    end 
    return false 
end 
  
car = {} 
  
function vehicle(vehicle) 
    DestruirVehiculo(source, _) 
     
    if not getPedOccupiedVehicle( source ) then 
        local vehicleID = getVehicleIDFromName(vehicle) 
        local x,y,z = getElementPosition( source ); x=x+1 
         
        car[source] = createVehicle( vehicleID, x, y, z, 0, 0, 180); 
        warpPedIntoVehicle(source, car[source]) 
    end 
        return true; 
end 
addEvent("onCrearVehiculo", true) 
addEventHandler("onCrearVehiculo", root,vehicle) 
  
function DestruirVehiculo(player) 
if eventName then player = source end 
if car[player] then 
    destroyElement(car[player]) 
    car[player] = nil 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout",root, DestruirVehiculo) 
  

Link to comment

...

function DestruirVehiculo( ) 
    if car[ player ] then 
        if isElement( car[ player ] ) then 
            destroyElement( car[ player ] ) 
            car[ player ] = nil 
        end 
        car[ player ] = nil 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout", getRootElement( ), DestruirVehiculo) 

Link to comment
...

function DestruirVehiculo( ) 
    if car[ player ] then 
        if isElement( car[ player ] ) then 
            destroyElement( car[ player ] ) 
            car[ player ] = nil 
        end 
        car[ player ] = nil 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout", getRootElement( ), DestruirVehiculo) 

La linea 7 no es necesaria.

Link to comment
...

function DestruirVehiculo( ) 
    if car[ player ] then 
        if isElement( car[ player ] ) then 
            destroyElement( car[ player ] ) 
            car[ player ] = nil 
        end 
        car[ player ] = nil 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout", getRootElement( ), DestruirVehiculo) 

No nada, sigue lo mismo y no entiendo el porque, subiré client y sever hay esta todo el código prueba y veras:

ventanaP = guiCreateStaticImage(0.40, 0.33, 0.20, 0.33, ":ReproductorCoches/imagen/Fondo.png", true) 
guiSetAlpha(ventanaP, 0.81) 
  
listado = guiCreateGridList(0.03, 0.02, 0.94, 0.65, true, ventanaP) 
         
--Obtener = guiCreateButton(0.03, 0.70, 0.45, 0.13, "Obtener", true, ventanaP) 
Cerrar = guiCreateButton(0.03, 0.70, 0.94, 0.13, "Cerrar", true, ventanaP)   
sitio = guiCreateLabel(0.03, 0.93, 0.84, 0.04, "http://comunidadlatina.eshost.com.ar/", true, ventanaP) 
guiSetVisible(ventanaP, false) 
  
----------Texto en 3D---------------------- 
local screenW, screenH = guiGetScreenSize() 
  
function text() 
    dxDrawText("Reproductor de carros", (screenW * 0.3541) - 1, (screenH * 0.2782) - 1, (screenW * 0.6476) - 1, (screenH * 0.3377) - 1, tocolor(45, 217, 37, 255), 2.00, "pricedown", "left", "top", false, false, true, false, false) 
    dxDrawText("Reproductor de carros", (screenW * 0.3541) + 1, (screenH * 0.2782) - 1, (screenW * 0.6476) + 1, (screenH * 0.3377) - 1, tocolor(45, 217, 37, 255), 2.00, "pricedown", "left", "top", false, false, true, false, false) 
    dxDrawText("Reproductor de carros", (screenW * 0.3541) - 1, (screenH * 0.2782) + 1, (screenW * 0.6476) - 1, (screenH * 0.3377) + 1, tocolor(45, 217, 37, 255), 2.00, "pricedown", "left", "top", false, false, true, false, false) 
    dxDrawText("Reproductor de carros", (screenW * 0.3541) + 1, (screenH * 0.2782) + 1, (screenW * 0.6476) + 1, (screenH * 0.3377) + 1, tocolor(45, 217, 37, 255), 2.00, "pricedown", "left", "top", false, false, true, false, false) 
    dxDrawText("Reproductor de carros", screenW * 0.3541, screenH * 0.2782, screenW * 0.6476, screenH * 0.3377, tocolor(255, 255, 255, 255), 2.00, "pricedown", "left", "top", false, false, true, false, false) 
end 
------------------------------------------- 
  
function abrirCerrar() --Abrimos y cerramos la ventana 
    if (guiGetVisible(ventanaP) == false) then 
        addEventHandler ( "onClientRender", root, text ) -- keep the text visible with onClientRender. 
        guiSetVisible(ventanaP, true) 
        showCursor(true)     
    else 
        removeEventHandler("onClientRender", root, text) 
        guiSetVisible(ventanaP, false) 
        showCursor(false) 
    end 
end 
addEvent("mostrarPanel", true ) -- Agregamos un evento cuando ingresemos al marker nos abra el panel 
addEventHandler("mostrarPanel", root, abrirCerrar) 
addEventHandler("onClientGUIClick", Cerrar, abrirCerrar, false) -- Boton cerrar 
  
------------Columnas----------------- 
guiGridListSetSelectionMode(listado,1) 
a=guiGridListAddColumn(listado, "Id", 0.4) 
b=guiGridListAddColumn(listado, "Vehiculo", 0.5) 
------------------------------------------------------------- 
  
tblCoches={ 
{445}, 
{481}, 
{458}, 
} 
------------- Almacenamos los coches en la lista --------------------     
for i,v in pairs (tblCoches) do  
    local vehicle = getVehicleNameFromID(v[1]) 
    local row = guiGridListAddRow (listado) 
    guiGridListSetItemText(listado,i-1,a,i,false,false) 
    guiGridListSetItemText (listado, row, b, vehicle, false, true) 
end 
      
local player = getLocalPlayer() 
            
function Seleccion() 
    if source == listado then --Puedes cambiarlo a un boton, pero recuerda cambiar el onClientDoubleClick por onClientGUIClick si vas a usar botones 
        playSoundFrontEnd (2) 
        local row, column = guiGridListGetSelectedItem(listado) 
        if ( row ~= guiGridListGetRowCount ( listado ) and column ~= 0 ) then 
            local vehicleID = guiGridListGetItemText ( listado, row, b ) 
            triggerServerEvent("onCrearVehiculo", player, vehicleID) 
            abrirCerrar() 
        end 
    end 
end 
addEventHandler("onClientGUIDoubleClick", root, Seleccion) 
  

server:

marker = {} 
  
function Marcador( element ) 
    if getElementType( element ) == "player" then 
        local veh = getPedOccupiedVehicle( element ) 
        if veh then 
            return  
        else 
            triggerClientEvent(element, "mostrarPanel", element) 
        end 
    end 
end 
  
----------Crear Marcador y comprobamos si es administrador--------------------- 
function crearMarcador(thePlayer, cmd) 
local obtCuent = getAccountName(getPlayerAccount(thePlayer)) -- Obtenemos el nombre de la cuenta 
    if isObjectInACLGroup("user."..obtCuent, aclGetGroup("Admin"))  then -- Comparamos si esta en el grupo de administradores 
        local marcadores = LeerTxt(thePlayer) 
        for i = 1, #marcadores do 
            local x, y, z = unpack(marcadores[i]) --tipo, tamano, r, g, b, a 
            if not marker[i] then 
                marker[i] = createMarker(x, y, z, "cylinder", 2, 0 ,255, 0, 155, root ) 
                createBlip( x, y, z, 51, 0, 0, 0, 255, 0, 0, 300.0, source ) 
                addEventHandler("onMarkerHit", marker[i], Marcador) --Llamamos la funcion Marcador cuando un jugador toca el marker 
            end 
        end 
    end 
end 
  
  
function crearTxt(thePlayer) ------------------- Creamos el archivo y lo escribimos, obtener la posición del marcador ----------------------------------  
  local x, y, z = getElementPosition(thePlayer) -- Obtenemos la posición del jugador 
    local position = tostring(x) .. ", " .. tostring(y) ..", ".. tostring(z-1) .."\r\n" 
  
    if fileExists ( "posicion.txt" ) then 
        local file = fileOpen("posicion.txt") 
        fileSetPos ( file, fileGetSize ( file ) ) 
        fileWrite(file, position) 
        fileClose(file) 
    else 
        local file = fileCreate("posicion.txt") 
        fileWrite(file, position) 
        fileClose(file) 
    end 
    crearMarcador(thePlayer) 
end 
addCommandHandler("crear", crearTxt) 
  
function LeerTxt(Player) 
    local marcadores = {} 
    local hFile = fileOpen("posicion.txt", true) 
    
    if hFile then                                   
        buffer = fileRead(hFile, fileGetSize(hFile))     
        strtable = split(buffer, "\r\n")         
        for i = 1, #strtable do 
            local x = tonumber(gettok(strtable[i], 1, ",")) 
            local y = tonumber(gettok(strtable[i], 2, ",")) 
            local z = tonumber(gettok(strtable[i], 3, ",")) 
            marcadores[i] = {x, y, z} 
        end     
        fileClose(hFile) 
        return marcadores 
    else 
        outputConsole("Unable to open test.txt") 
    end 
    return false 
end 
  
car = {} 
  
function vehicle(vehicle) 
    DestruirVehiculo(source, _) 
     
    if not getPedOccupiedVehicle( source ) then 
        local vehicleID = getVehicleIDFromName(vehicle) 
        local x,y,z = getElementPosition( source ); x=x+1 
         
        car[source] = createVehicle( vehicleID, x, y, z, 0, 0, 180); 
        warpPedIntoVehicle(source, car[source]) 
    end 
        return true; 
end 
addEvent("onCrearVehiculo", true) 
addEventHandler("onCrearVehiculo", root,vehicle) 
  
function DestruirVehiculo( player ) 
    if car[ player ] then 
        if isElement( car[ player ] ) then 
            destroyElement( car[ player ] ) 
            car[ player ] = nil 
        end 
        car[ player ] = nil 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout", getRootElement( ), DestruirVehiculo) 
  
  
--[[function DestruirVehiculo(player) 
if eventName then player = source end 
if car[player] then 
    destroyElement(car[player]) 
    car[player] = nil 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout",root, DestruirVehiculo)]]-- 
  
  
--[[function DestruirVehiculo(source, _) 
if car[source] then 
    destroyElement(car[source]) 
    car[source] = nil 
    end 
end 
addCommandHandler("dv", DestruirVehiculo) 
addEventHandler("onPlayerLogout",source, DestruirVehiculo)]]-- 
  

Link to comment
  • Recently Browsing   0 members

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