Jump to content

Problema con onClientPlayerWeaponFire y SetElementData


Narutimmy

Recommended Posts

Hola tengo un problema :S

Pasa que lo tengo dividido los IF entre Armas Primarias, secundarios y tercerias...

El problema es que si disparo la primera me resta las balas de la primera (bien) pero tambien de la secundaria y tercearia...

igual si lanzo una granada (tercearia) me resta balas a la primaria... y asi.

Si ago un solo if se detiene la verificacion, osea mmm

Si por ejemplo no tengo arma primaria equipada verifica la segunda y si si tengo sedunda ya no verifica la tercera.

si si tengo la primera no verifica segunda y tercera.

Como puedo hacer :S

function weaponSwitch(weapon)  
     
  
     
   if source == getLocalPlayer() then     
   ----PRIMARIWEAPON 
    local wpn = getElementData(getLocalPlayer(), "currentweapon_1")  
    if wpn == "BM16" then  
        if getElementData(getLocalPlayer(), "BM16 Pellet") > 0 then  
         setElementData(getLocalPlayer(), "BM16 Pellet", getElementData(getLocalPlayer(), "BM16 Pellet") - 1)  
        end 
     
  
    elseif wpn == ".50 Barret" then  
        if getElementData(getLocalPlayer(), ".50 Barret Mag") > 0 then  
         setElementData(getLocalPlayer(), ".50 Barret Mag", getElementData(getLocalPlayer(), ".50 Barret Mag") - 1)  
        end 
     
     
    elseif wpn == "Sig550" then  
        if getElementData(getLocalPlayer(), "Sig550 Mag") > 0 then  
         setElementData(getLocalPlayer(), "Sig550 Mag", getElementData(getLocalPlayer(), "Sig550 Mag") - 1)  
        end 
     
     
    elseif wpn == "Gauss Rifle" then  
        if getElementData(getLocalPlayer(), "Gauss Rifle Mag") > 0 then  
         setElementData(getLocalPlayer(), "Gauss Rifle Mag", getElementData(getLocalPlayer(), "Gauss Rifle Mag") - 1)  
        end 
     
     
    elseif wpn == "M-14 Camouflage Grid" then  
        if getElementData(getLocalPlayer(), "M-14 Camouflage Grid Mag") > 0 then  
         setElementData(getLocalPlayer(), "M-14 Camouflage Grid Mag", getElementData(getLocalPlayer(), "M-14 Camouflage Grid Mag") - 1)  
        end 
     
     
    elseif wpn == "Thompson 1928" then  
        if getElementData(getLocalPlayer(), "Thompson 1928 Mag") > 0 then  
         setElementData(getLocalPlayer(), "Thompson 1928 Mag", getElementData(getLocalPlayer(), "Thompson 1928 Mag") - 1)  
        end 
     
    elseif wpn == "Type 99" then  
        if getElementData(getLocalPlayer(), "Type 99 Mag") > 0 then  
         setElementData(getLocalPlayer(), "Type 99 Mag", getElementData(getLocalPlayer(), "Type 99 Mag") - 1)  
        end 
         
    elseif wpn == "Ak74" then  
        if getElementData(getLocalPlayer(), "AK Mag") > 0 then  
         setElementData(getLocalPlayer(), "AK Mag", getElementData(getLocalPlayer(), "AK Mag") - 1)  
        end 
     
     
    elseif wpn == "Hammerhead 47" then  
        if getElementData(getLocalPlayer(), "14mm Ammo") > 0 then  
         setElementData(getLocalPlayer(), "14mm Ammo", getElementData(getLocalPlayer(), "14mm Ammo") - 1)  
        end 
     
    elseif wpn == "LR300" then  
        if getElementData(getLocalPlayer(), "LR300 Mag") > 0 then  
         setElementData(getLocalPlayer(), "LR300 Mag", getElementData(getLocalPlayer(), "LR300 Mag") - 1)  
        end 
     
    elseif wpn == "GAU-19" then  
        if getElementData(getLocalPlayer(), "GAU-19 Mag") > 0 then  
         setElementData(getLocalPlayer(), "GAU-19 Mag", getElementData(getLocalPlayer(), "GAU-19 Mag") - 1)  
        end 
     
    elseif wpn == "TOS 34" then  
        if getElementData(getLocalPlayer(), "TOS 34 Pellet") > 0 then  
         setElementData(getLocalPlayer(), "TOS 34 Pellet", getElementData(getLocalPlayer(), "TOS 34 Pellet") - 1)  
        end 
     
    elseif wpn == "SPSA 14" then  
        if getElementData(getLocalPlayer(), "SPSA 14 Pellet") > 0 then  
         setElementData(getLocalPlayer(), "SPSA 14 Pellet", getElementData(getLocalPlayer(), "SPSA 14 Pellet") - 1)  
        end 
     
    elseif wpn == "SVDm 2" then  
        if getElementData(getLocalPlayer(), "SVDm 2 Mag") > 0 then  
         setElementData(getLocalPlayer(), "SVDm 2 Mag", getElementData(getLocalPlayer(), "SVDm 2 Mag") - 1)  
        end 
     
    elseif wpn == "Ballesta" then  
        if getElementData(getLocalPlayer(), "Ballesta Mag") > 0 then  
         setElementData(getLocalPlayer(), "Ballesta Mag", getElementData(getLocalPlayer(), "Ballesta Mag") - 1)  
        end 
     
    elseif wpn == "Heat-Seeking RPG" then  
        if getElementData(getLocalPlayer(), "M136 Rocket") > 0 then  
         setElementData(getLocalPlayer(), "M136 Rocket", getElementData(getLocalPlayer(), "M136 Rocket") - 1)  
        end 
     
    elseif wpn == "M136 Rocket Launcher" then  
        if getElementData(getLocalPlayer(), "M136 Rocket") > 0 then  
         setElementData(getLocalPlayer(), "M136 Rocket", getElementData(getLocalPlayer(), "M136 Rocket") - 1)  
        end 
    end 
     
    ---SECUNDARYWEAPON 
    local wpn2 = getElementData(getLocalPlayer(), "currentweapon_2")  
    if wpn2 == "Nailgun" then  
        if getElementData(getLocalPlayer(), "Nails") > 0 then  
         setElementData(getLocalPlayer(), "Nails", getElementData(getLocalPlayer(), "Nails") - 1)  
        end 
     
    elseif wpn2 == "Taser" then  
        if getElementData(getLocalPlayer(), "Taser Battery") > 0 then  
         setElementData(getLocalPlayer(), "Taser Battery", getElementData(getLocalPlayer(), "Taser Battery") - 1)  
        end 
     
    --- 
    elseif wpn2 == "PB1s" then  
        if getElementData(getLocalPlayer(), "PB1s Mag") > 0 then  
         setElementData(getLocalPlayer(), "PB1s Mag", getElementData(getLocalPlayer(), "PB1s Mag") - 1)  
        end 
     
    elseif wpn2 == "Berta" then  
        if getElementData(getLocalPlayer(), "Berta Mag") > 0 then  
         setElementData(getLocalPlayer(), "Berta Mag", getElementData(getLocalPlayer(), "Berta Mag") - 1)  
        end 
     
    elseif wpn2 == "Desert Eagle" then  
        if getElementData(getLocalPlayer(), "Desert Eagle") > 0 then  
         setElementData(getLocalPlayer(), "Desert Eagle Mag", getElementData(getLocalPlayer(), "Desert Eagle Mag") - 1)  
        end 
     
    elseif wpn2 == "PDW" then  
        if getElementData(getLocalPlayer(), "PDW Mag") > 0 then  
         setElementData(getLocalPlayer(), "PDW Mag", getElementData(getLocalPlayer(), "PDW Mag") - 1)  
        end 
     
    elseif wpn2 == "Viper 5" then  
        if getElementData(getLocalPlayer(), "Viper 5") > 0 then  
         setElementData(getLocalPlayer(), "Viper 5 Mag", getElementData(getLocalPlayer(), "Viper 5 Mag") - 1)  
        end 
    end 
     
    ---TERCER WAPON  
     
     
    --- 
    local wpn3 = getElementData(getLocalPlayer(), "currentweapon_3")  
    if wpn3 == "Parachute" then  
        if getElementData(getLocalPlayer(), "Parachute") > 0 then  
         setElementData(getLocalPlayer(), "Parachute", getElementData(getLocalPlayer(), "Parachute") - 1)  
        end 
     
    elseif wpn3 == "Grenade" then  
        if getElementData(getLocalPlayer(), "Grenade") > 0 then  
         setElementData(getLocalPlayer(), "Grenade", getElementData(getLocalPlayer(), "Grenade") - 1)  
        end 
     
    elseif wpn3 == "Tear Gas" then  
        if getElementData(getLocalPlayer(), "Tear Gas") > 0 then  
         setElementData(getLocalPlayer(), "Tear Gas", getElementData(getLocalPlayer(), "Tear Gas") - 1)  
        end 
    end 
   end  
end  
  
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), weaponSwitch) 

Link to comment

Los elseif son leídos si el IF de arriba returna false, es como un else pero con él puedes no se como decirlo... comparar, comprobar, esas cosas que haces con un if ej:

  
function lalala() 
if (1+1) == 3 then 
print("The code says: 1+1 = 3") -- Esto nunca se interpretará porque 1+1 = 2 y en el IF indica que si 1+1 = 3 esto aparecerá. 
elseif (2+2) = 4 then 
print("The code says: 2+2 = 4") -- Como el primer IF returnó false se prosigue con el ELSEIF 
end 
end 
  

Link to comment
Los elseif son leídos si el IF de arriba returna false, es como un else pero con él puedes no se como decirlo... comparar, comprobar, esas cosas que haces con un if ej:
  
function lalala() 
if (1+1) == 3 then 
print("The code says: 1+1 = 3") -- Esto nunca se interpretará porque 1+1 = 2 y en el IF indica que si 1+1 = 3 esto aparecerá. 
elseif (2+2) = 4 then 
print("The code says: 2+2 = 4") -- Como el primer IF returnó false se prosigue con el ELSEIF 
end 
end 
  

mas o menos eso fue lo que dije pero no lo se explicar xD, en fin que podria hacer?

Link to comment

¿Probaste a hacerlo así?:

  
    local wpn = getElementData(getLocalPlayer(), "currentweapon_1") 
    if wpn == "BM16" then 
        if getElementData(getLocalPlayer(), "BM16 Pellet") > 0 then 
         setElementData(getLocalPlayer(), "BM16 Pellet", getElementData(getLocalPlayer(), "BM16 Pellet") - 1) 
        end 
    end 
    
  
    if wpn == ".50 Barret" then 
        if getElementData(getLocalPlayer(), ".50 Barret Mag") > 0 then 
         setElementData(getLocalPlayer(), ".50 Barret Mag", getElementData(getLocalPlayer(), ".50 Barret Mag") - 1) 
        end 
    end 
  

Link to comment
¿Probaste a hacerlo así?:
  
    local wpn = getElementData(getLocalPlayer(), "currentweapon_1") 
    if wpn == "BM16" then 
        if getElementData(getLocalPlayer(), "BM16 Pellet") > 0 then 
         setElementData(getLocalPlayer(), "BM16 Pellet", getElementData(getLocalPlayer(), "BM16 Pellet") - 1) 
        end 
    end 
    
  
    if wpn == ".50 Barret" then 
        if getElementData(getLocalPlayer(), ".50 Barret Mag") > 0 then 
         setElementData(getLocalPlayer(), ".50 Barret Mag", getElementData(getLocalPlayer(), ".50 Barret Mag") - 1) 
        end 
    end 
  

si de echo fue la primera forma que intente funciona bien pero se desaparece municion del inventario

Link to comment

Para esto lo mas simple es usar tablas.

De 150 lineas te lo reduje a 30 aprox:

weapons = {} 
weapons[1] = {"BM16 Pellet",".50 Barret Mag","Sig550 Mag","Gauss Rifle Mag","M-14 Camouflage Grid Mag", 
              "Thompson 1928 Mag","Type 99 Mag","AK Mag","14mm Ammo","LR300 Mag","GAU-19 Mag","TOS 34 Pellet", 
              "SPSA 14 Pellet","SVDm 2 Mag", "Ballesta Mag","M136 Rocket","M136 Rocket"} 
weapons[2] = {"Nails","Taser Battery","PB1s Mag","Berta Mag","Desert Eagle","PDW Mag","Viper 5"} 
weapons[3] = {"Parachute","Grenade","Tear Gas"} 
  
wpn = {} 
wpn[1] = {"BM16",".50 Barret","Sig550","Gauss Rifle","M-14 Camouflage Grid","Thompson 1928","Type 99", 
          "Ak74","Hammerhead 47","LR300","GAU-19","TOS 34","SPSA 14","SVDm 2","Ballesta","Heat-Seeking RPG", 
          "M136 Rocket Launcher"} 
wpn[2] = {"Nailgun","Taser","PB1s","Berta","Desert Eagle","PDW","Viper 5"} 
wpn[3] = {"Parachute","Grenade","Tear Gas"} 
  
function weaponSwitch(weapon) 
   if source == getLocalPlayer() then   
       for i = 1, 3 do 
            for j = 1, #weapons[i] do 
                local wpnID = getElementData(getLocalPlayer(), "currentweapon_"..i) 
                if wpnID == wpn[i][j] then 
                    if getElementData(getLocalPlayer(), weapons[i][j]) > 0 then 
                        setElementData(getLocalPlayer(), weapons[i][j], getElementData(getLocalPlayer(), weapons[i][j]) - 1) 
                    end 
                end 
            end 
        end 
    end 
end 
  
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), weaponSwitch) 

Link to comment
Para esto lo mas simple es usar tablas.

De 150 lineas te lo reduje a 30 aprox:

weapons = {} 
weapons[1] = {"BM16 Pellet",".50 Barret Mag","Sig550 Mag","Gauss Rifle Mag","M-14 Camouflage Grid Mag", 
              "Thompson 1928 Mag","Type 99 Mag","AK Mag","14mm Ammo","LR300 Mag","GAU-19 Mag","TOS 34 Pellet", 
              "SPSA 14 Pellet","SVDm 2 Mag", "Ballesta Mag","M136 Rocket","M136 Rocket"} 
weapons[2] = {"Nails","Taser Battery","PB1s Mag","Berta Mag","Desert Eagle","PDW Mag","Viper 5"} 
weapons[3] = {"Parachute","Grenade","Tear Gas"} 
  
wpn = {} 
wpn[1] = {"BM16",".50 Barret","Sig550","Gauss Rifle","M-14 Camouflage Grid","Thompson 1928","Type 99", 
          "Ak74","Hammerhead 47","LR300","GAU-19","TOS 34","SPSA 14","SVDm 2","Ballesta","Heat-Seeking RPG", 
          "M136 Rocket Launcher"} 
wpn[2] = {"Nailgun","Taser","PB1s","Berta","Desert Eagle","PDW","Viper 5"} 
wpn[3] = {"Parachute","Grenade","Tear Gas"} 
  
function weaponSwitch(weapon) 
   if source == getLocalPlayer() then   
       for i = 1, 3 do 
            for j = 1, #weapons[i] do 
                local wpnID = getElementData(getLocalPlayer(), "currentweapon_"..i) 
                if wpnID == wpn[i][j] then 
                    if getElementData(getLocalPlayer(), weapons[i][j]) > 0 then 
                        setElementData(getLocalPlayer(), weapons[i][j], getElementData(getLocalPlayer(), weapons[i][j]) - 1) 
                    end 
                end 
            end 
        end 
    end 
end 
  
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), weaponSwitch) 

wow... no sabia que se podia hacer eso, lo voy a probar y te aviso, tendras alguna guia de como usarlas?

EDIT: Bueno e probado y de funcionar funciona en cuanto a que 150 lines a 30. pero el bug persiste :/

Link to comment

Quizás con un break te funcione, aunque no se si funciona el break en lua.

weapons = {} 
weapons[1] = {"BM16 Pellet",".50 Barret Mag","Sig550 Mag","Gauss Rifle Mag","M-14 Camouflage Grid Mag", 
              "Thompson 1928 Mag","Type 99 Mag","AK Mag","14mm Ammo","LR300 Mag","GAU-19 Mag","TOS 34 Pellet", 
              "SPSA 14 Pellet","SVDm 2 Mag", "Ballesta Mag","M136 Rocket","M136 Rocket"} 
weapons[2] = {"Nails","Taser Battery","PB1s Mag","Berta Mag","Desert Eagle","PDW Mag","Viper 5"} 
weapons[3] = {"Parachute","Grenade","Tear Gas"} 
  
wpn = {} 
wpn[1] = {"BM16",".50 Barret","Sig550","Gauss Rifle","M-14 Camouflage Grid","Thompson 1928","Type 99", 
          "Ak74","Hammerhead 47","LR300","GAU-19","TOS 34","SPSA 14","SVDm 2","Ballesta","Heat-Seeking RPG", 
          "M136 Rocket Launcher"} 
wpn[2] = {"Nailgun","Taser","PB1s","Berta","Desert Eagle","PDW","Viper 5"} 
wpn[3] = {"Parachute","Grenade","Tear Gas"} 
  
function weaponSwitch(weapon) 
   if source == getLocalPlayer() then   
       for i = 1, 3 do 
            for j = 1, #weapons[i] do 
                local wpnID = getElementData(getLocalPlayer(), "currentweapon_"..i) 
                if wpnID == wpn[i][j] then 
                    if getElementData(getLocalPlayer(), weapons[i][j]) > 0 then 
                        setElementData(getLocalPlayer(), weapons[i][j], getElementData(getLocalPlayer(), weapons[i][j]) - 1) 
                        break 
                    end 
                end 
            end 
        end 
    end 
end 
  
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), weaponSwitch) 

Link to comment
Quizás con un break te funcione, aunque no se si funciona el break en lua.
weapons = {} 
weapons[1] = {"BM16 Pellet",".50 Barret Mag","Sig550 Mag","Gauss Rifle Mag","M-14 Camouflage Grid Mag", 
              "Thompson 1928 Mag","Type 99 Mag","AK Mag","14mm Ammo","LR300 Mag","GAU-19 Mag","TOS 34 Pellet", 
              "SPSA 14 Pellet","SVDm 2 Mag", "Ballesta Mag","M136 Rocket","M136 Rocket"} 
weapons[2] = {"Nails","Taser Battery","PB1s Mag","Berta Mag","Desert Eagle","PDW Mag","Viper 5"} 
weapons[3] = {"Parachute","Grenade","Tear Gas"} 
  
wpn = {} 
wpn[1] = {"BM16",".50 Barret","Sig550","Gauss Rifle","M-14 Camouflage Grid","Thompson 1928","Type 99", 
          "Ak74","Hammerhead 47","LR300","GAU-19","TOS 34","SPSA 14","SVDm 2","Ballesta","Heat-Seeking RPG", 
          "M136 Rocket Launcher"} 
wpn[2] = {"Nailgun","Taser","PB1s","Berta","Desert Eagle","PDW","Viper 5"} 
wpn[3] = {"Parachute","Grenade","Tear Gas"} 
  
function weaponSwitch(weapon) 
   if source == getLocalPlayer() then   
       for i = 1, 3 do 
            for j = 1, #weapons[i] do 
                local wpnID = getElementData(getLocalPlayer(), "currentweapon_"..i) 
                if wpnID == wpn[i][j] then 
                    if getElementData(getLocalPlayer(), weapons[i][j]) > 0 then 
                        setElementData(getLocalPlayer(), weapons[i][j], getElementData(getLocalPlayer(), weapons[i][j]) - 1) 
                        break 
                    end 
                end 
            end 
        end 
    end 
end 
  
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), weaponSwitch) 

he Probado y tampoco funcione me parece que es porque al tener arma Primaria y Secundaria ("currentweapon_") esta pasando, lo que hace falta es algo que verifique que arma tengo equipada y en base a eso continuar.

EDIT:

Dudo funcione pero me refiero a algo asi... como lo ago?

weapons = {} 
weapons[1] = {"BM16 Pellet",".50 Barret Mag","Sig550 Mag","Gauss Rifle Mag","M-14 Camouflage Grid Mag", 
              "Thompson 1928 Mag","Type 99 Mag","AK Mag","14mm Ammo","LR300 Mag","GAU-19 Mag","TOS 34 Pellet", 
              "SPSA 14 Pellet","SVDm 2 Mag", "Ballesta Mag","M136 Rocket","M136 Rocket"} 
weapons[2] = {"Nails","Taser Battery","PB1s Mag","Berta Mag","Desert Eagle","PDW Mag","Viper 5"} 
weapons[3] = {"Parachute","Grenade","Tear Gas"} 
  
wpn = {} 
wpn[1] = {"BM16",".50 Barret","Sig550","Gauss Rifle","M-14 Camouflage Grid","Thompson 1928","Type 99", 
          "Ak74","Hammerhead 47","LR300","GAU-19","TOS 34","SPSA 14","SVDm 2","Ballesta","Heat-Seeking RPG", 
          "M136 Rocket Launcher"} 
wpn[2] = {"Nailgun","Taser","PB1s","Berta","Desert Eagle","PDW","Viper 5"} 
wpn[3] = {"Parachute","Grenade","Tear Gas"} 
  
Primary[1] = {"25,26,27,30,31,33,34,35,36,37,38"} 
Secundary[2] = {"1,2,3,4,5,6,7,8,22,23,24,28,29,32"} 
Tercery[3] = {"16,17,18,39,46"} 
  
function weaponSwitch(weapon) 
   if source == getLocalPlayer() then   
       for i = 1, 3 do 
            for j = 1, #weapons[i] do 
                local wpnID = getElementData(getLocalPlayer(), "currentweapon_"..i) 
                if wpnID == wpn[i][j] then 
                 if weapon == Primary[1] then 
                    if getElementData(getLocalPlayer(), weapons[1]) > 0 then 
                        setElementData(getLocalPlayer(), weapons[1], getElementData(getLocalPlayer(), weapons[1]) - 1) 
                        break 
                    end 
                    elseif weapon == Secundary[1] then 
                    if getElementData(getLocalPlayer(), weapons[2]) > 0 then 
                        setElementData(getLocalPlayer(), weapons[2], getElementData(getLocalPlayer(), weapons[2]) - 1) 
                        break 
                    end 
                    elseif weapon == Tercery[3] then 
                    if getElementData(getLocalPlayer(), weapons[3]) > 0 then 
                        setElementData(getLocalPlayer(), weapons[3], getElementData(getLocalPlayer(), weapons[3]) - 1) 
                        break 
                    end 
                  end 
                end 
            end 
        end 
    end 
end 
  
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), weaponSwitch) 

Link to comment

Creo que no entiendes bien el concepto de un for, simplemente es un for por cada dimensión, si deseas agregar otra dimensión tienes que agregar otro for, así te ahorras poner tantos elseif. La verdad no entendí mucho lo que quisiste hacer. Prueba esto:

PD: te recomiendo que aprendas a trabajar con tablas y for, es difícil de entender, pero te simplifican mucho los trabajos.

weapons = {} 
weapons[1] = {"BM16 Pellet",".50 Barret Mag","Sig550 Mag","Gauss Rifle Mag","M-14 Camouflage Grid Mag", 
              "Thompson 1928 Mag","Type 99 Mag","AK Mag","14mm Ammo","LR300 Mag","GAU-19 Mag","TOS 34 Pellet", 
              "SPSA 14 Pellet","SVDm 2 Mag", "Ballesta Mag","M136 Rocket","M136 Rocket"} 
weapons[2] = {"Nails","Taser Battery","PB1s Mag","Berta Mag","Desert Eagle","PDW Mag","Viper 5"} 
weapons[3] = {"Parachute","Grenade","Tear Gas"} 
  
wpn = {} 
wpn[1] = {"BM16",".50 Barret","Sig550","Gauss Rifle","M-14 Camouflage Grid","Thompson 1928","Type 99", 
          "Ak74","Hammerhead 47","LR300","GAU-19","TOS 34","SPSA 14","SVDm 2","Ballesta","Heat-Seeking RPG", 
          "M136 Rocket Launcher"} 
wpn[2] = {"Nailgun","Taser","PB1s","Berta","Desert Eagle","PDW","Viper 5"} 
wpn[3] = {"Parachute","Grenade","Tear Gas"} 
  
current={} 
current[1] = {"25,26,27,30,31,33,34,35,36,37,38"} 
current[2] = {"1,2,3,4,5,6,7,8,22,23,24,28,29,32"} 
current[3] = {"16,17,18,39,46"} 
  
function weaponSwitch(weapon) 
   if source == getLocalPlayer() then   
       for i = 1, 3 do 
            for j = 1, #weapons[i] do 
                local wpnID = getElementData(getLocalPlayer(), "currentweapon_"..i) 
                if wpnID == wpn[i][j] then 
                    for k = 1, 3 do 
                        if weapon == current[k][j] then 
                            if getElementData(getLocalPlayer(), weapons[i][j]) > 0 then 
                                setElementData(getLocalPlayer(), weapons[i][j], getElementData(getLocalPlayer(), weapons[i][j]) - 1) 
                            end 
                        end 
                    end 
                end 
            end 
        end 
    end 
end 
  
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), weaponSwitch) 

Link to comment
  • MTA Team
Los elseif son leídos si el IF de arriba returna false, es como un else pero con él puedes no se como decirlo... comparar, comprobar, esas cosas que haces con un if

Los elseif son leídos aunque el primer IF devuelva true o false. La única manera que no sea leído es si interrumpes el condicional.

En cuanto al break si funciona en Lua, se utiliza para interrumpir un loop. Si consideramos que un loop por ejemplo, "loopea" unas 5 veces, y queremos interrumpir el loop a la 3era vez, utilizamos un break. En otras palabras completa el loop antes de tiempo.

El while se utiliza como condicional en un loop, para establecer que mientras un valor es verdadero (true), se sigue ejecutando el loop, de lo contrario no lo hará.

Link to comment

Fue una manera muy sucia y probe a prueba y error... pero... Funciono xD

Gracias por ayudarme :D

weapons = {} 
weapons[1] = {"BM16 Pellet",".50 Barret Mag","Sig550 Mag","Gauss Rifle Mag","M-14 Camouflage Grid Mag", 
              "Thompson 1928 Mag","Type 99 Mag","AK Mag","14mm Ammo","LR300 Mag","GAU-19 Mag","TOS 34 Pellet", 
              "SPSA 14 Pellet","SVDm 2 Mag", "Ballesta Mag","M136 Rocket","M136 Rocket"} 
weapons[2] = {"Nails","Taser Battery","PB1s Mag","Berta Mag","Desert Eagle","PDW Mag","Viper 5"} 
weapons[3] = {"Parachute","Grenade","Tear Gas"} 
  
wpn = {} 
wpn[1] = {"BM16",".50 Barret","Sig550","Gauss Rifle","M-14 Camouflage Grid","Thompson 1928","Type 99", 
          "Ak74","Hammerhead 47","LR300","GAU-19","TOS 34","SPSA 14","SVDm 2","Ballesta","Heat-Seeking RPG", 
          "M136 Rocket Launcher"} 
wpn[2] = {"Nailgun","Taser","PB1s","Berta","Desert Eagle","PDW","Viper 5"} 
wpn[3] = {"Parachute","Grenade","Tear Gas"} 
  
  
  
function weaponSwitch(weapon) 
   if source == getLocalPlayer() then   
       for i = 1, 3 do 
            for j = 1, #weapons[i] do 
            local ID = 0 
            if weapon == 25  then 
            ID = 1 
            elseif weapon == 26 then 
            ID = 1 
            elseif weapon ==  27  then 
            ID = 1 
            elseif weapon ==  29  then 
            ID = 1 
            elseif weapon ==  30  then 
            ID = 1 
            elseif weapon == 31  then 
            ID = 1 
            elseif weapon == 33  then 
            ID = 1 
            elseif weapon == 33  then 
            ID = 1 
            elseif weapon == 35  then 
            ID = 1 
            elseif weapon == 36  then 
            ID = 1 
            elseif weapon == 37  then 
            ID = 1 
            elseif weapon == 38 then 
            ID = 1 
            elseif weapon == 1  then 
            ID = 2 
            elseif weapon ==  2  then 
            ID = 2 
            elseif weapon ==  3  then 
            ID = 2 
            elseif weapon ==  4  then 
            ID = 2 
            elseif weapon ==  5  then 
            ID = 2 
            elseif weapon ==  6  then 
            ID = 2 
            elseif weapon ==  7  then 
            ID = 2 
            elseif weapon ==  8  then 
            ID = 2 
            elseif weapon ==  22  then 
            ID = 2 
            elseif weapon ==  24  then 
            ID = 2 
            elseif weapon ==  28  then 
            ID = 2 
            elseif weapon ==  29  then 
            ID = 2 
            elseif weapon ==  32 then 
            ID = 2 
            elseif weapon ==  16 then 
            ID = 3 
            elseif weapon ==  17  then 
            ID = 3 
            elseif weapon ==  18  then 
            ID = 3 
            elseif weapon ==  39  then 
            ID = 3 
            elseif weapon ==  46 then 
            ID = 3 
            end 
                local wpnID = getElementData(getLocalPlayer(), "currentweapon_"..ID) 
                if wpnID == wpn[i][j] then 
                    if getElementData(getLocalPlayer(), weapons[i][j]) > 0 then 
                        setElementData(getLocalPlayer(), weapons[i][j], getElementData(getLocalPlayer(), weapons[i][j]) - 1) 
                        break 
                    end 
                end 
            end 
        end 
    end 
end 
  
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), weaponSwitch) 

PD: Alguien sabe simplicar esto xD intente con "or" pero no funcionaba.

Link to comment
  • MTA Team

Rápidamente simplificado podría ser así:

            if weapon == 25 or weapon == 26 or weapon ==  27 or weapon ==  29 or weapon ==  30 or weapon == 31 or weapon == 33 or weapon == 35 or weapon == 36 or weapon == 37 or weapon == 38 then 
            ID = 1 
            elseif weapon == 1 or weapon ==  2 or weapon ==  3 or weapon ==  3 or weapon ==  4 or weapon ==  5 or weapon ==  6 or weapon ==  7 or weapon ==  8 or weapon ==  22 or weapon ==  24 or weapon ==  28 or weapon ==  29 or weapon ==  32 then 
            ID = 2 
            elseif weapon ==  16 or weapon ==  17 or weapon ==  18 or weapon ==  39 or weapon ==  46 then 
            ID = 3 
            end 

De igual manera, es una manera muy poco eficiente.

Link to comment

Ahora comprendi lo que querias hacer, podrías crear una función aparte para que re retorne el ID, asi seria mas simple:

weapons = {} 
weapons[1] = {"BM16 Pellet",".50 Barret Mag","Sig550 Mag","Gauss Rifle Mag","M-14 Camouflage Grid Mag", 
              "Thompson 1928 Mag","Type 99 Mag","AK Mag","14mm Ammo","LR300 Mag","GAU-19 Mag","TOS 34 Pellet", 
              "SPSA 14 Pellet","SVDm 2 Mag", "Ballesta Mag","M136 Rocket","M136 Rocket"} 
weapons[2] = {"Nails","Taser Battery","PB1s Mag","Berta Mag","Desert Eagle","PDW Mag","Viper 5"} 
weapons[3] = {"Parachute","Grenade","Tear Gas"} 
  
wpn = {} 
wpn[1] = {"BM16",".50 Barret","Sig550","Gauss Rifle","M-14 Camouflage Grid","Thompson 1928","Type 99", 
          "Ak74","Hammerhead 47","LR300","GAU-19","TOS 34","SPSA 14","SVDm 2","Ballesta","Heat-Seeking RPG", 
          "M136 Rocket Launcher"} 
wpn[2] = {"Nailgun","Taser","PB1s","Berta","Desert Eagle","PDW","Viper 5"} 
wpn[3] = {"Parachute","Grenade","Tear Gas"} 
  
current={} 
current[1] = {"25,26,27,30,31,33,34,35,36,37,38"} 
current[2] = {"1,2,3,4,5,6,7,8,22,23,24,28,29,32"} 
current[3] = {"16,17,18,39,46"} 
  
function weaponSwitch(weapon) 
   if source == getLocalPlayer() then   
       for i = 1, 3 do 
            for j = 1, #weapons[i] do 
                local ID = searchID(weapon) 
                local wpnID = getElementData(getLocalPlayer(), "currentweapon_"..ID) 
                if wpnID == wpn[i][j] then 
                    if getElementData(getLocalPlayer(), weapons[i][j]) > 0 then 
                        setElementData(getLocalPlayer(), weapons[i][j], getElementData(getLocalPlayer(), weapons[i][j]) - 1) 
                    end 
                end 
            end 
        end 
    end 
end 
  
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), weaponSwitch) 
  
function searchID(weapon) 
   local find = false 
   local ID = false 
   for i = 1, 3 do 
        for j = 1, #current[i] do 
            if weapon == current[i][j] then 
                ID = i 
                find = true 
                break 
            end 
        end 
        if find then 
            break 
        end 
    end 
    return ID 
end 

Link to comment
  • Recently Browsing   0 members

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