Jump to content

[Ayuda] Variable, Spawn de Autos


Narutimmy

Recommended Posts

Hola bueno tengo un problema, tengo para que con un comando spawneen un auto, pero para que no se sature quiero que al spawnear uno nuevo desaparesca el antrerior, el problema es que me elimina el anterior globalmente y no el que el spawneo.

Quiero asignarle una variable dinamica para que no suseda ese problema..

ejemplo:

client

function spawnauto (thePlayer) 
local x, y, z = getElementPosition(thePlayer) 
if veh then 
destroyElement(veh) 
veh = createVehicle(433, x + 5, y, z) 
end 
end 
addCommandHandler("spawn", spawnauto ) 
  
  

ahi funciona bien pero si el el user 2 spawnea otro elimina el auto del user 1...

La idea seria que la variable "veh" sea dinamica por ejemplo "vehNarutimmy" para el user 1 y "vehTroler" para el user 2. que la variable la obtenga del nick del user que usa el comando.

Link to comment

Intenta con esto:

local veh = {} 
  
function spawnauto (thePlayer) 
    local x, y, z = getElementPosition(thePlayer) 
        if veh[thePlayer] then 
            destroyElement(veh[thePlayer]) 
        end 
            veh[thePlayer] = createVehicle(433, x, y, z+0.1) 
            warpPedIntoVehicle( thePlayer, veh[thePlayer] ) 
end 
addCommandHandler("veh", spawnauto ) 
  
addEventHandler( 'onPlayerQuit', root, 
    function() 
        if veh[source] then 
            destroyElement(veh[source]) 
            veh[source] = nil 
        end 
    end 
) 

La tabla 'veh' tiene como key al jugador y como value al vehículo, al momento de usar el comando '/veh', revisas si en la tabla existe un vehículo asignado al jugador, si es así, este se elimina, luego se crea otro y se le asigna al jugador en la tabla, también añadí una función para eliminarlo al irse.

Te dejo este tutorial que quizá te enseñe un poco de tablas:

http://lua-users.org/wiki/TablesTutorial

*Todo esto es server-side.

Link to comment
  • 1 month later...
Intenta con esto:
local veh = {} 
  
function spawnauto (thePlayer) 
    local x, y, z = getElementPosition(thePlayer) 
        if veh[thePlayer] then 
            destroyElement(veh[thePlayer]) 
        end 
            veh[thePlayer] = createVehicle(433, x, y, z+0.1) 
            warpPedIntoVehicle( thePlayer, veh[thePlayer] ) 
end 
addCommandHandler("veh", spawnauto ) 
  
addEventHandler( 'onPlayerQuit', root, 
    function() 
        if veh[source] then 
            destroyElement(veh[source]) 
            veh[source] = nil 
        end 
    end 
) 

La tabla 'veh' tiene como key al jugador y como value al vehículo, al momento de usar el comando '/veh', revisas si en la tabla existe un vehículo asignado al jugador, si es así, este se elimina, luego se crea otro y se le asigna al jugador en la tabla, también añadí una función para eliminarlo al irse.

Te dejo este tutorial que quizá te enseñe un poco de tablas:

http://lua-users.org/wiki/TablesTutorial

*Todo esto es server-side.

necesito ayuda me da un buen de errores... cuando salgo no elimina el auto y demas cosas...

  addEventHandler( 'onPlayerQuit', root, 
    function() 
        if veh[source] then 
            destroyElement(veh[source]) 
            destroyElement(vehCol[source]) 
            veh[source] = nil 
            vehCol[source] = nil 
        end 
    end 
) 
  
local veh = {} 
local vehCol = {} 
  
function SpawnDayZPatriot(thePlayer) 
  
  accountname = getAccountName(getPlayerAccount(thePlayer)) 
    if (getElementData( thePlayer, 'V.I.P.0' ) == true) or (getElementData( thePlayer, 'V.I.P.3' ) == true) or (getElementData( thePlayer, 'V.I.P.4' ) == true) and (getElementData(thePlayer,"AAutoS") == 0) then 
    if veh[thePlayer] then 
        destroyElement(veh[thePlayer]) 
        destroyElement(vehCol[thePlayer]) 
        outputChatBox("#6002B1[VSC]#FF0000-Tu anterior Auto a sido Destruido!", thePlayer, 171, 205, 239, true) 
  
      end    
      local x, y, z = getElementPosition(thePlayer) 
      setElementData(thePlayer,"AAutoS",1) 
      veh[thePlayer] = createVehicle(470, x +5, y, z) 
      vehCol[thePlayer] = createColSphere(x +5, y, z, 2.5) 
      attachElements(vehCol[thePlayer], veh[thePlayer], 0, 0, 0) 
      setElementData(vehCol[thePlayer], "parent", veh[thePlayer]) 
      setElementData(veh[thePlayer], "parent", vehCol[thePlayer]) 
      setElementData(vehCol[thePlayer], "vehicle", true) 
      setElementData(vehCol[thePlayer], "MAX_Slots", 10) 
      local tires, engine, parts = getVehicleAddonInfos(getElementModel(veh[thePlayer])) 
      setElementData(vehCol[thePlayer], "Tire_inVehicle", 4) 
      setElementData(vehCol[thePlayer], "Engine_inVehicle", 1) 
      setElementData(vehCol[thePlayer], "Parts_inVehicle", 1) 
        setElementData(vehCol[thePlayer],"VIPCAR", true) 
     
      setElementData(vehCol[thePlayer], "fuel", 100) 
      setTimer(outputChatBox, 50, 1, "#6002B1[VSC]#FFFFFF-Patriot has been spawned!", thePlayer, 171, 205, 239, true) 
    
  else 
    outputChatBox("#6002B1[VSC]#FF0000-No eres VIP, o has sobrepasado tu limite, 1 auto por Vida!", thePlayer, 171, 205, 239, true) 
  end 
end 
  
addCommandHandler("/patriotown", SpawnDayZPatriot) 
addCommandHandler("Patriot",SpawnDayZPatriot) 
vehicleAddonsInfo = { 
  { 
    470, 
    4, 
    1, 
    1 
  } 
} 
function getVehicleAddonInfos(id) 
  for i, veh in ipairs(vehicleAddonsInfo) do 
    if veh[1] == id then 
      return veh[2], veh[thePlayer][3], veh[thePlayer][4] 
    end 
  end 
end 
vehicleFuelInfo = { 
  {470, 0.1} 
} 
function getVehicleAddonInfos(id) 
  for i, veh in ipairs(vehicleAddonsInfo) do 
    if veh[1] == id then 
      return veh[thePlayer][2], veh[thePlayer][3], veh[thePlayer][4] 
    end 
  end 
end 
vehicleFuelTable = { 
  {470, 100} 
} 
function getVehicleMaxFuel(loot) 
  local modelID = getElementModel(getElementData(loot, "parent")) 
  for i, vehicle in ipairs(vehicleFuelTable) do 
    if modelID == vehicle[1] then 
      return vehicle[2] 
    end 
  end 
  return false 
end 
function onPlayerEnterDayzVehicle(veh, seat) 
  local col = getElementData(dayZVeh, "parent") 
  local id = getElementModel(dayZVeh) 
  if not seat == 1 then 
    return 
  end 
  local tires, engine, parts = getVehicleAddonInfos(id) 
  setVehicleEngineState(dayZVeh, false) 
  setElementData(dayZVeh, "maxfuel", getVehicleMaxFuel(col)) 
  setElementData(dayZVeh, "needtires", tires) 
  setElementData(dayZVeh, "needengines", engine) 
  setElementData(dayZVeh, "needparts", parts) 
  if getElementData(col, "Tire_inVehicle") or 0 < tonumber(tires) then 
    setVehicleEngineState(dayZVeh, false) 
    return 
  end 
  if getElementData(col, "Engine_inVehicle") or 0 < tonumber(engine) then 
    setVehicleEngineState(dayZVeh, false) 
    return 
  end 
  if not getElementData(col, "Parts_inVehicle") then 
    setElementData(col, "Parts_inVehicle", math.random(0, parts)) 
  end 
  if getElementData(col, "fuel") or 1 >= 0 and not getElementModel(dayZVehBob) == 509 then 
    triggerClientEvent(source, "displayClientInfo", source, "Vehicle", "No tank left in this vehicle!", 22, 255, 0) 
    setVehicleEngineState(dayZVehBob, false) 
    return 
  end 
  end 
  

Link to comment
Para ese error, simplemente mueve el primer evento mas abajo. Porque esta llamando a una tabla que aun no ha sido creada.

Saludos.

lo puse asi, vere si funciona.

local veh = {} 
local vehCol = {} 
local Spawneed = false 
  
function SpawnDayZPatriot(thePlayer) 
  
  accountname = getAccountName(getPlayerAccount(thePlayer)) 
    if (getElementData( thePlayer, 'V.I.P.0' ) == true) or (getElementData( thePlayer, 'V.I.P.3' ) == true) or (getElementData( thePlayer, 'V.I.P.4' ) == true) and (getElementData(thePlayer,"AAutoS") == 0) then 
    if veh[thePlayer] then 
        destroyElement(veh[thePlayer]) 
        destroyElement(vehCol[thePlayer]) 
        outputChatBox("#6002B1[VSC]#FF0000-Tu anterior Auto a sido Destruido!", thePlayer, 171, 205, 239, true) 
  
      end    
      local x, y, z = getElementPosition(thePlayer) 
      setElementData(thePlayer,"AAutoS",1) 
      Spawneed = true 
      veh[thePlayer] = createVehicle(470, x +5, y, z) 
      vehCol[thePlayer] = createColSphere(x +5, y, z, 2.5) 
      attachElements(vehCol[thePlayer], veh[thePlayer], 0, 0, 0) 
      setElementData(vehCol[thePlayer], "parent", veh[thePlayer]) 
      setElementData(veh[thePlayer], "parent", vehCol[thePlayer]) 
      setElementData(vehCol[thePlayer], "vehicle", true) 
      setElementData(vehCol[thePlayer], "MAX_Slots", 10) 
      local tires, engine, parts = getVehicleAddonInfos(getElementModel(veh[thePlayer])) 
      setElementData(vehCol[thePlayer], "Tire_inVehicle", 4) 
      setElementData(vehCol[thePlayer], "Engine_inVehicle", 1) 
      setElementData(vehCol[thePlayer], "Parts_inVehicle", 1) 
        setElementData(vehCol[thePlayer],"VIPCAR", true) 
     
      setElementData(vehCol[thePlayer], "fuel", 100) 
      setTimer(outputChatBox, 50, 1, "#6002B1[VSC]#FFFFFF-Patriot has been spawned!", thePlayer, 171, 205, 239, true) 
    
  else 
    outputChatBox("#6002B1[VSC]#FF0000-No eres VIP, o has sobrepasado tu limite, 1 auto por Vida!", thePlayer, 171, 205, 239, true) 
  end 
end 
  
addCommandHandler("/patriotown", SpawnDayZPatriot) 
addCommandHandler("Patriot",SpawnDayZPatriot) 
vehicleAddonsInfo = { 
  { 
    470, 
    4, 
    1, 
    1 
  } 
} 
function getVehicleAddonInfos(id) 
  for i, veh in ipairs(vehicleAddonsInfo) do 
    if veh[1] == id then 
      return veh[2], veh[thePlayer][3], veh[thePlayer][4] 
    end 
  end 
end 
vehicleFuelInfo = { 
  {470, 0.1} 
} 
function getVehicleAddonInfos(id) 
  for i, veh in ipairs(vehicleAddonsInfo) do 
    if veh[1] == id then 
      return veh[thePlayer][2], veh[thePlayer][3], veh[thePlayer][4] 
    end 
  end 
end 
vehicleFuelTable = { 
  {470, 100} 
} 
function getVehicleMaxFuel(loot) 
  local modelID = getElementModel(getElementData(loot, "parent")) 
  for i, vehicle in ipairs(vehicleFuelTable) do 
    if modelID == vehicle[1] then 
      return vehicle[2] 
    end 
  end 
  return false 
end 
function onPlayerEnterDayzVehicle(veh, seat) 
  local col = getElementData(dayZVeh, "parent") 
  local id = getElementModel(dayZVeh) 
  if not seat == 1 then 
    return 
  end 
  local tires, engine, parts = getVehicleAddonInfos(id) 
  setVehicleEngineState(dayZVeh, false) 
  setElementData(dayZVeh, "maxfuel", getVehicleMaxFuel(col)) 
  setElementData(dayZVeh, "needtires", tires) 
  setElementData(dayZVeh, "needengines", engine) 
  setElementData(dayZVeh, "needparts", parts) 
  if getElementData(col, "Tire_inVehicle") or 0 < tonumber(tires) then 
    setVehicleEngineState(dayZVeh, false) 
    return 
  end 
  if getElementData(col, "Engine_inVehicle") or 0 < tonumber(engine) then 
    setVehicleEngineState(dayZVeh, false) 
    return 
  end 
  if not getElementData(col, "Parts_inVehicle") then 
    setElementData(col, "Parts_inVehicle", math.random(0, parts)) 
  end 
  if getElementData(col, "fuel") or 1 >= 0 and not getElementModel(dayZVehBob) == 509 then 
    triggerClientEvent(source, "displayClientInfo", source, "Vehicle", "No tank left in this vehicle!", 22, 255, 0) 
    setVehicleEngineState(dayZVehBob, false) 
    return 
  end 
  end 
   
  ---============================== 
   
    addEventHandler( 'onPlayerQuit', root, 
    function() 
    if (Spawneed == true) then 
        if veh[source] then 
            destroyElement(veh[source]) 
            destroyElement(vehCol[source]) 
            veh[source] = nil 
            vehCol[source] = nil 
        end 
    end 
    end 
) 
  

EDIT: listo ya esta :)

Link to comment
  • Recently Browsing   0 members

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