Jump to content

[Ayuda] setTimer y Tablas.


Platin

Recommended Posts

Posted

Bueno, hice un script muy sencillo hace un tiempo, pero como no había terminado de entender el funcionamiento de las tablas no lo seguí. Al comprobar con un CMD que puedo crear autos que sean de una persona y quitarlos si pone más de uno correctamente, procedí a intentar terminar el script inicial, pero no pude.

El script es sencillo, crea una c4 que luego de 15 segundos explotará.

Cabe aclarar que es triggeada de client > server | por cuestiones de mi manera de scriptear.

server-side

function bombaPoner() 
    if not bombas[source] then 
        setPedAnimation (source, "BOMBER", "BOM_Plant", 4000, false, true, false) 
        x, y, z = getElementPosition ( source ) 
        rx, ry, rz = getElementRotation ( source ) 
        bombas[source] = createObject( 1252, x, y, z, rx, ry, rz, false) 
        cx, cy, cz = getElementPosition ( bombas[source] ) 
        outputChatBox("#FFFF99¡La bomba explotará en #00FF0015 segundos#FFFF99! ¡CORRE!", source, 255, 0, 0, true) 
        plr = source 
        setTimer(function() 
        destroyElement(bombas[source]) 
        createExplosion ( cx, cy, cz, 10, plr ) 
        end, 15000, 1, bombas[source], plr) -- Esta linea es el problema, ya que no me lleva el objeto, por lo cual no se destruye, ¿cómo lo puedo arreglar? 
    end 
    --outputChatBox("Deshabilitado",source,255,0,0) 
end 
addEvent("bombaColocar", true) 
addEventHandler( "bombaColocar", root, bombaPoner ) 

PD: En el script dice el error.

Espero una respuesta, gracias de antemano, ¡saludos!

Posted

Trata con esto

bombas = {} 
  
function bombaPoner() 
        setPedAnimation (source, "BOMBER", "BOM_Plant", 4000, false, true, false) 
        x, y, z = getElementPosition ( source ) 
        rx, ry, rz = getElementRotation ( source ) 
        bombas[source] = createObject( 1252, x, y, z, rx, ry, rz, false) 
        outputChatBox("#FFFF99¡La bomba explotará en #00FF0015 segundos#FFFF99! ¡CORRE!", source, 255, 0, 0, true) 
        detonarBomba(bombas[source]) 
    --outputChatBox("Deshabilitado",source,255,0,0) 15000 
end 
addEvent("bombaColocar", true) 
addEventHandler( "bombaColocar", root, bombaPoner ) 
  
function detonarBomba(element) 
    setTimer(function() 
        if isElement(element) then 
        local cx, cy, cz = getElementPosition ( element ) 
        createExplosion ( cx, cy, cz, 10, source ) 
        destroyElement(element) 
        end 
    end, 15000, 1) 
end 

Posted
Trata con esto
bombas = {} 
  
function bombaPoner() 
        setPedAnimation (source, "BOMBER", "BOM_Plant", 4000, false, true, false) 
        x, y, z = getElementPosition ( source ) 
        rx, ry, rz = getElementRotation ( source ) 
        bombas[source] = createObject( 1252, x, y, z, rx, ry, rz, false) 
        outputChatBox("#FFFF99¡La bomba explotará en #00FF0015 segundos#FFFF99! ¡CORRE!", source, 255, 0, 0, true) 
        detonarBomba(bombas[source]) 
    --outputChatBox("Deshabilitado",source,255,0,0) 15000 
end 
addEvent("bombaColocar", true) 
addEventHandler( "bombaColocar", root, bombaPoner ) 
  
function detonarBomba(element) 
    setTimer(function() 
        if isElement(element) then 
        local cx, cy, cz = getElementPosition ( element ) 
        createExplosion ( cx, cy, cz, 10, source ) 
        destroyElement(element) 
        end 
    end, 15000, 1) 
end 

Me sirvió, muchas gracias.

Posted
No problema :D

Bueno, ahora probando me di cuenta que solo funciona 1 vez, a la siguiente no ejecuta nada.

¿Tienes alguna idea del por qué?

Posted

en el ultimo argumento del setTimer en vez de un 1 ponle un 0, este represeta infinitas repeticiones.

Posted
en el ultimo argumento del setTimer en vez de un 1 ponle un 0, este represeta infinitas repeticiones.

¿Eso no iría a ocasionar errores? Digo, no todo el tiempo hay una bomba, y si se coloca una luego de haberse iniciado el settimer habrán 2 setTimers.

Posted

Bueno, ahora probando me di cuenta que solo funciona 1 vez, a la siguiente no ejecuta nada.

¿Tienes alguna idea del por qué?

Es extraño, debería funcionar perfectamente, sin embargo intenta definir un argumento en vez de source.

¿Eso no iría a ocasionar errores? Digo, no todo el tiempo hay una bomba, y si se coloca una luego de haberse iniciado el settimer habrán 2 setTimers.

Y no, no ocasionaría errores, no se repetirá la explosión.

Posted
No problema :D

Bueno, ahora probando me di cuenta que solo funciona 1 vez, a la siguiente no ejecuta nada.

¿Tienes alguna idea del por qué?

Lo probé y si funciona o.O

Posted
en el ultimo argumento del setTimer en vez de un 1 ponle un 0, este represeta infinitas repeticiones.

Eso no sería nada eficiente, repetir una función infinitas veces para ver si algo existe...

Posted

Coloca esto SOLO si es triggeado desde client y cuenta si ha funcionado, no estoy seguro de hacerlo correctamente

    bombas = {} 
      
    function bombaPoner() 
            setPedAnimation (client, "BOMBER", "BOM_Plant", 4000, false, true, false) 
            x, y, z = getElementPosition ( client) 
            rx, ry, rz = getElementRotation ( client) 
            bombas[source] = createObject( 1252, x, y, z, rx, ry, rz, false) 
            outputChatBox("#FFFF99¡La bomba explotará en #00FF0015 segundos#FFFF99! ¡CORRE!", client, 255, 0, 0, true) 
            detonarBomba(bombas[client]) 
        --outputChatBox("Deshabilitado",client,255,0,0) 15000 
    end 
    addEvent("bombaColocar", true) 
    addEventHandler( "bombaColocar", root, bombaPoner ) 
      
    function detonarBomba(element_bomba) 
        setTimer(function() 
            if isElement(element_bomba) then 
            local cx, cy, cz = getElementPosition ( element_bomba) 
            createExplosion ( cx, cy, cz, 10) 
            destroyElement(element_bomba) 
            end 
        end, 15000, 1) 
    end 

Posted
Coloca esto SOLO si es triggeado desde client y cuenta si ha funcionado, no estoy seguro de hacerlo correctamente
    bombas = {} 
      
    function bombaPoner() 
            setPedAnimation (client, "BOMBER", "BOM_Plant", 4000, false, true, false) 
            x, y, z = getElementPosition ( client) 
            rx, ry, rz = getElementRotation ( client) 
            bombas[source] = createObject( 1252, x, y, z, rx, ry, rz, false) 
            outputChatBox("#FFFF99¡La bomba explotará en #00FF0015 segundos#FFFF99! ¡CORRE!", client, 255, 0, 0, true) 
            detonarBomba(bombas[client]) 
        --outputChatBox("Deshabilitado",client,255,0,0) 15000 
    end 
    addEvent("bombaColocar", true) 
    addEventHandler( "bombaColocar", root, bombaPoner ) 
      
    function detonarBomba(element_bomba) 
        setTimer(function() 
            if isElement(element_bomba) then 
            local cx, cy, cz = getElementPosition ( element_bomba) 
            createExplosion ( cx, cy, cz, 10) 
            destroyElement(element_bomba) 
            end 
        end, 15000, 1) 
    end 

Los argumentos como client, ped, element, vehicle, object y entre otros, tambien sirven para argumentos. Tecnicamente haces lo mismo.

PD: HE PROBADO MI CODIGO Y FUNCIONA.

Posted
Coloca esto SOLO si es triggeado desde client y cuenta si ha funcionado, no estoy seguro de hacerlo correctamente
    bombas = {} 
      
    function bombaPoner() 
            setPedAnimation (client, "BOMBER", "BOM_Plant", 4000, false, true, false) 
            x, y, z = getElementPosition ( client) 
            rx, ry, rz = getElementRotation ( client) 
            bombas[source] = createObject( 1252, x, y, z, rx, ry, rz, false) 
            outputChatBox("#FFFF99¡La bomba explotará en #00FF0015 segundos#FFFF99! ¡CORRE!", client, 255, 0, 0, true) 
            detonarBomba(bombas[client]) 
        --outputChatBox("Deshabilitado",client,255,0,0) 15000 
    end 
    addEvent("bombaColocar", true) 
    addEventHandler( "bombaColocar", root, bombaPoner ) 
      
    function detonarBomba(element_bomba) 
        setTimer(function() 
            if isElement(element_bomba) then 
            local cx, cy, cz = getElementPosition ( element_bomba) 
            createExplosion ( cx, cy, cz, 10) 
            destroyElement(element_bomba) 
            end 
        end, 15000, 1) 
    end 

Los argumentos como client, ped, element, vehicle, object y entre otros, tambien sirven para argumentos. Tecnicamente haces lo mismo.

PD: HE PROBADO MI CODIGO Y FUNCIONA.

Funciona 1 vez, al segundo triggeo deja de funcionar, como que el elemento ya no puede ser creado / destruido. Ese es el error en concreto, paso clientside:

function bombaC () 
    if not bomba then 
        bomba = true 
        setTimer (function() 
        bomba = false 
        end, 30000, 1) 
        triggerServerEvent("bombaColocar", localPlayer) 
    else 
        outputChatBox("#FFFF99¡Ya pusiste una bomba! ¡Espera un tiempo!", 255, 255, 255, true) 
    end 
end 
addCommandHandler ( "bomba", bombaC ) 
addCommandHandler ( "c4", bombaC ) 

Posted

Prueba con esta versión que hice:

bomb = {}; 
function createBomb(player, x, y, z, delay ) 
    if not isBombExists(bomb[player]) then 
        bomb[player] = createObject( 1252, x, y, z, 0,0,0, false); 
        setPedAnimation (player, "BOMBER", "BOM_Plant", 4000, false, true, false); 
        outputChatBox("#FFFF99¡La bomba explotará en #00FF0015 segundos#FFFF99! ¡CORRE!", player, 255, 0, 0, true); 
            setTimer(function()  
                local x, y, z = getElementPosition ( bomb[player] ); 
                createExplosion ( x, y, z, 10 ); 
                destroyElement( bomb[player] ); 
                bomb[player] = nil 
            end, delay, 1 ); 
        else 
        outputChatBox("#FFFF99¡Ya pusiste una bomba! ¡Espera un tiempo!", player, 255, 255, 255, true); 
    end 
end 
  
function isBombExists( element ) 
    if element then return true end 
end 
  
function bomba( thePlayer ) 
    local x, y, z = getElementPosition ( thePlayer ); 
    createBomb(thePlayer, x, y, z, 15000); 
end 
addCommandHandler("bomba", bomba) 
addCommandHandler("c4", bomba) 

source

createBomb( el_creador, x, y, z, tiempo_detonacion ) 

Posted
Prueba con esta versión que hice:
bomb = {}; 
function createBomb(player, x, y, z, delay ) 
    if not isBombExists(bomb[player]) then 
        bomb[player] = createObject( 1252, x, y, z, 0,0,0, false); 
        setPedAnimation (player, "BOMBER", "BOM_Plant", 4000, false, true, false); 
        outputChatBox("#FFFF99¡La bomba explotará en #00FF0015 segundos#FFFF99! ¡CORRE!", player, 255, 0, 0, true); 
            setTimer(function()  
                local x, y, z = getElementPosition ( bomb[player] ); 
                createExplosion ( x, y, z, 10 ); 
                destroyElement( bomb[player] ); 
                bomb[player] = nil 
            end, delay, 1 ); 
        else 
        outputChatBox("#FFFF99¡Ya pusiste una bomba! ¡Espera un tiempo!", player, 255, 255, 255, true); 
    end 
end 
  
function isBombExists( element ) 
    if element then return true end 
end 
  
function bomba( thePlayer ) 
    local x, y, z = getElementPosition ( thePlayer ); 
    createBomb(thePlayer, x, y, z, 15000); 
end 
addCommandHandler("bomba", bomba) 
addCommandHandler("c4", bomba) 

source

createBomb( el_creador, x, y, z, tiempo_detonacion ) 

Funciona, gracias.

Lástima que no se pudo mantener lo de esperar el doble de tiempo :S

Posted
Prueba con esta versión que hice:
bomb = {}; 
function createBomb(player, x, y, z, delay ) 
    if not isBombExists(bomb[player]) then 
        bomb[player] = createObject( 1252, x, y, z, 0,0,0, false); 
        setPedAnimation (player, "BOMBER", "BOM_Plant", 4000, false, true, false); 
        outputChatBox("#FFFF99¡La bomba explotará en #00FF0015 segundos#FFFF99! ¡CORRE!", player, 255, 0, 0, true); 
            setTimer(function()  
                local x, y, z = getElementPosition ( bomb[player] ); 
                createExplosion ( x, y, z, 10 ); 
                destroyElement( bomb[player] ); 
                bomb[player] = nil 
            end, delay, 1 ); 
        else 
        outputChatBox("#FFFF99¡Ya pusiste una bomba! ¡Espera un tiempo!", player, 255, 255, 255, true); 
    end 
end 
  
function isBombExists( element ) 
    if element then return true end 
end 
  
function bomba( thePlayer ) 
    local x, y, z = getElementPosition ( thePlayer ); 
    createBomb(thePlayer, x, y, z, 15000); 
end 
addCommandHandler("bomba", bomba) 
addCommandHandler("c4", bomba) 

source

createBomb( el_creador, x, y, z, tiempo_detonacion ) 

Funciona, gracias.

Lástima que no se pudo mantener lo de esperar el doble de tiempo :S

De nada, puedes editar la parte del comando para que hagas lo de tu "tiempo de espera".

Posted

Algo como asi?

[ CREDITOS A ENARGY ]

tiempo = {} 
bomb = {} 
_delay = 15 --Segundos 
function createBomb(player, x, y, z, delay ) 
    if not isBombExists(bomb[player]) then 
    bomb[player] = createObject( 1252, x, y, z, 0,0,0, false) 
    setPedAnimation (player, "BOMBER", "BOM_Plant", 4000, false, true, false) 
    outputChatBox("#FFFF99¡La bomba explotará en #00FF0015 segundos#FFFF99! ¡CORRE!", player, 255, 0, 0, true) 
    tiempo[player] = tonumber(delay)*2 
    setTimer(function() 
        local x, y, z = getElementPosition ( bomb[player] ) 
        createExplosion ( x, y, z, 10 ) 
        destroyElement( bomb[player] ) 
        bomb[player] = nil 
        end, delay*1000, 1 ) 
    --else 
    --outputChatBox("#FFFF99¡Ya pusiste una bomba! ¡Espera un tiempo!", player, 255, 255, 255, true) 
    end 
end 
      
function isBombExists( element ) 
    if element then return true end 
end 
  
function bomba( thePlayer ) 
    if tiempo[thePlayer] <= 0 then 
    local x, y, z = getElementPosition ( thePlayer ) 
    createBomb(thePlayer, x, y, z, _delay) 
    else 
    outputChatBox("#FFFF99¡Ya pusiste una bomba! ¡Espera un tiempo!", player, 255, 255, 255, true) 
    end 
end 
addCommandHandler("bomba", bomba) 
addCommandHandler("c4", bomba) 
  
function red () 
    for k,v in ipairs(getElementsByType("player")) do 
        if tiempo[v] ~= nil then 
        tiempo[v] = tiempo[v] - 1 
        end 
    end 
end 
setTimer(red, 1000, 0) 

Posted
Algo como asi?

[ CREDITOS A ENARGY ]

tiempo = {} 
bomb = {} 
_delay = 15 --Segundos 
function createBomb(player, x, y, z, delay ) 
    if not isBombExists(bomb[player]) then 
    bomb[player] = createObject( 1252, x, y, z, 0,0,0, false) 
    setPedAnimation (player, "BOMBER", "BOM_Plant", 4000, false, true, false) 
    outputChatBox("#FFFF99¡La bomba explotará en #00FF0015 segundos#FFFF99! ¡CORRE!", player, 255, 0, 0, true) 
    tiempo[player] = tonumber(delay)*2 
    setTimer(function() 
        local x, y, z = getElementPosition ( bomb[player] ) 
        createExplosion ( x, y, z, 10 ) 
        destroyElement( bomb[player] ) 
        bomb[player] = nil 
        end, delay*1000, 1 ) 
    --else 
    --outputChatBox("#FFFF99¡Ya pusiste una bomba! ¡Espera un tiempo!", player, 255, 255, 255, true) 
    end 
end 
      
function isBombExists( element ) 
    if element then return true end 
end 
  
function bomba( thePlayer ) 
    if tiempo[thePlayer] <= 0 then 
    local x, y, z = getElementPosition ( thePlayer ) 
    createBomb(thePlayer, x, y, z, _delay) 
    else 
    outputChatBox("#FFFF99¡Ya pusiste una bomba! ¡Espera un tiempo!", player, 255, 255, 255, true) 
    end 
end 
addCommandHandler("bomba", bomba) 
addCommandHandler("c4", bomba) 
  
function red () 
    for k,v in ipairs(getElementsByType("player")) do 
        if tiempo[v] ~= nil then 
        tiempo[v] = tiempo[v] - 1 
        end 
    end 
end 
setTimer(red, 1000, 0) 

  
function red () 
    for k,v in ipairs(getElementsByType("player")) do 
        if tiempo[v] ~= nil then 
        tiempo[v] = tiempo[v] - 1 
        end 
    end 
end 
setTimer(red, 1000, 0) 
  

¿?¿?¿?¿?

Posted

Dejenlo así, no hay drama :P

Es que no quiero crear otro setTimer, este script era solo de prueba, tampoco necesito que sea "perfecto".

Gracias a todos nuevamente.

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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