Jump to content

Ipairs iterator


DRW

Recommended Posts

Posted

Hello, I'm using ipairs to create various objects with the same name, but I want to destroy the exact element that I've used, I mean, if 20 objects are created with the ipairs iterator and I damage one of them, the object that I've damaged has to be destroyed, instead of that, the element and marker don't get destroyed. What can I do?

Client

addEventHandler ("onClientObjectDamage", getRootElement(), function(loss,attacker) 
Zx,Zy,Zz= getElementPosition (source) 
weapons = getPedWeapon (localPlayer) 
if weapons == 5 and getElementData (source,"mineununtrium") then 
local uutt = getElementData (source,"qununtrium") 
setElementData (source,"qununtrium",uutt-1) 
if getElementData(source,"qununtrium") == 0 then 
triggerServerEvent ("give1",attacker) 
local sound1 = playSound3D ("sonido/roto.wav",Zx,Zy,Zz,false) 
outputChatBox ("#666666You have extracted #ffffff3 #666666units of #ffffffUnuntrium",255,255,255,true) 
end 
else 
cancelEvent() 
end 
end) 
  
  
  

Server

  
  
mines={} 
mines[1]={316.623046875, 2310.3662109375, 175.88377380371} 
mines[2]={368.55038452148, 2279.5004882812, 179.28540039062} 
mines[3]={371.35195922852, 2383.2370605469, 204.61744689941} 
mines[4]={322.07421875, 2455.6181640625, 209.29032897949} 
mines[5]={459.70449829102, 2427.0690917969, 191.66717529297} 
mines[6]={534.01818847656, 2469.5270996094, 183.58447265625} 
mines[7]={ 635.9619140625, 2403.568359375, 180.75654602051} 
mines[8]={629.318359375, 2513.05859375, 166.53332519531} 
mines[9]={ 757.48828125, 2565.681640625, 159.08197021484} 
mines[10]={881.20446777344, 2574.3151855469, 160.68569946289} 
mines[11]={778.15057373047, 2687.6740722656, 155.58869934082} 
mines[12]={958.9248046875, 2667.8623046875, 157.94715881348} 
mines[13]={912.7998046875, 2766.8828125, 182.83726501465} 
mines[14]={821.197265625, 2795.1171875, 166.05780029297} 
mines[15]={822.2978515625, 2455.7451171875, 140.25344848633} 
  
  
function createMarkerAttachedTo(element, mType, size, r, g, b, a, visibleTo, xOffset, yOffset, zOffset) 
    mType, size, r, g, b, a, visibleTo, xOffset, yOffset, zOffset = mType or "checkpoint", size or 4, r or 0, g or 0, b or 255, a or 255, visibleTo or getRootElement(), xOffset or 0, yOffset or 0, zOffset or 0 
    assert(isElement(element), "Bad argument @ 'createMarkerAttachedTo' [Expected element at argument 1, got " .. type(element) .. "]") assert(type(mType) == "string", "Bad argument @ 'createMarkerAttachedTo' [Expected string at argument 2, got " .. type(mType) .. "]") assert(type(size) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 3, got " .. type(size) .. "]") assert(type(r) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 4, got " .. type(r) .. "]") assert(type(g) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 5, got " .. type(g) .. "]") assert(type(b) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 6, got " .. type(b) .. "]") assert(type(a) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 7, got " .. type(a) .. "]") assert(isElement(visibleTo), "Bad argument @ 'createMarkerAttachedTo' [Expected element at argument 8, got " .. type(visibleTo) .. "]") assert(type(xOffset) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 9, got " .. type(xOffset) .. "]") assert(type(yOffset) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 10, got " .. type(yOffset) .. "]") assert(type(zOffset) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 11, got " .. type(zOffset) .. "]") 
    local m = createMarker(0, 0, 0, mType, size, r, g, b, a, visibleTo) 
    if m then if attachElements(m, element) then return m end end return false 
end 
  
  
addEvent ("objs",true) 
addEventHandler ("objs",getRootElement(),function() 
    for variable,minee in pairs(mines) do 
        yx,yy,yz=unpack(minee) 
object1 = createObject (3929,yx,yy,yz) 
setElementData (object1,"mineununtrium","true") 
setElementData(object1,"qununtrium",5) 
marke = createMarkerAttachedTo (object1,"corona",4,255,255,255,50,getRootElement(),0,0,10) 
end 
end) 
  
  
  
addEvent ("give1",true) 
addEventHandler ("give1",getRootElement(),function() 
local uut = getElementData (source,"ununtrium") 
destroyElement (marke) 
setElementData (source,"ununtrium",uut+3) 
end) 

tJ5zeFm.gif

Proud owner and developer of ZNEXT: Aftermath.

Enter a post-apocalyptic San Andreas and fight over 30 types of enemies and bosses with varying difficulties and skills, improve and customize your character by leveling up, completing challenges and a solid lootbox system with no Pay-to-Win mechanics that will break your experience.

Meet new characters, creatures and weapon metas, experience an innovative combo-based melee system, or join our solid PvP modes to show other survivors who’s boss. 

Español, Pусский, Türk, عربى, Polski, Português

IP: mtasa://104.36.110.227:22003 - Discord: https://discord.gg/CxMxjvC5pB

Posted

Any errors in dubugscript ?

I damage one of them, the object that I've damaged has to be destroyed, instead of that, the element and marker don't get destroyed. What can I do?

What you mean there ? You want to destroy the object, element and marker ?

Multi Theft Auto Player since middle of 2011.

Everybody want to act Crazy/Smart/Intelligent/Different/Unique/Innocent and Thats why I am Perfect.

Posted

With setElementData assign an ID (they must be related to you table indexes) to an object. Why do you need pairs if you iterate through indexes?

And please, tabulate your code. It looks very messy.

local objects = {} 
  
addEvent ("objs",true) 
addEventHandler ("objs",getRootElement(), 
    function() 
        for i, v in ipairs(mines) do 
            local x, y, z = unpack(v) 
            objects[i] = createObject(3929, x, y, z) 
            setElementData(objects[i], "id", i) 
            setElementData(objects[i], "mineununtrium", "true") 
            setElementData(objects[i], "qununtrium", 5) 
            local marker = createMarkerAttachedTo(objects[i], "corona", 4, 255, 255, 255, 50, getRootElement(), 0, 0, 10) 
        end 
    end 
) 

I hope you know what to do next.

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted
Any errors in dubugscript ?
I damage one of them, the object that I've damaged has to be destroyed, instead of that, the element and marker don't get destroyed. What can I do?

What you mean there ? You want to destroy the object, element and marker ?

Yes, I want to destroy the object along with the marker

tJ5zeFm.gif

Proud owner and developer of ZNEXT: Aftermath.

Enter a post-apocalyptic San Andreas and fight over 30 types of enemies and bosses with varying difficulties and skills, improve and customize your character by leveling up, completing challenges and a solid lootbox system with no Pay-to-Win mechanics that will break your experience.

Meet new characters, creatures and weapon metas, experience an innovative combo-based melee system, or join our solid PvP modes to show other survivors who’s boss. 

Español, Pусский, Türk, عربى, Polski, Português

IP: mtasa://104.36.110.227:22003 - Discord: https://discord.gg/CxMxjvC5pB

Posted

This should do the job.

  
mines={} 
mines[1]={316.623046875, 2310.3662109375, 175.88377380371} 
mines[2]={368.55038452148, 2279.5004882812, 179.28540039062} 
mines[3]={371.35195922852, 2383.2370605469, 204.61744689941} 
mines[4]={322.07421875, 2455.6181640625, 209.29032897949} 
mines[5]={459.70449829102, 2427.0690917969, 191.66717529297} 
mines[6]={534.01818847656, 2469.5270996094, 183.58447265625} 
mines[7]={ 635.9619140625, 2403.568359375, 180.75654602051} 
mines[8]={629.318359375, 2513.05859375, 166.53332519531} 
mines[9]={ 757.48828125, 2565.681640625, 159.08197021484} 
mines[10]={881.20446777344, 2574.3151855469, 160.68569946289} 
mines[11]={778.15057373047, 2687.6740722656, 155.58869934082} 
mines[12]={958.9248046875, 2667.8623046875, 157.94715881348} 
mines[13]={912.7998046875, 2766.8828125, 182.83726501465} 
mines[14]={821.197265625, 2795.1171875, 166.05780029297} 
mines[15]={822.2978515625, 2455.7451171875, 140.25344848633} 
  
objects = {} 
marke = {} 
  
function createMarkerAttachedTo(element, mType, size, r, g, b, a, visibleTo, xOffset, yOffset, zOffset) 
    mType, size, r, g, b, a, visibleTo, xOffset, yOffset, zOffset = mType or "checkpoint", size or 4, r or 0, g or 0, b or 255, a or 255, visibleTo or getRootElement(), xOffset or 0, yOffset or 0, zOffset or 0 
    assert(isElement(element), "Bad argument @ 'createMarkerAttachedTo' [Expected element at argument 1, got " .. type(element) .. "]")  
    assert(type(mType) == "string", "Bad argument @ 'createMarkerAttachedTo' [Expected string at argument 2, got " .. type(mType) .. "]")  
    assert(type(size) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 3, got " .. type(size) .. "]")  
    assert(type(r) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 4, got " .. type(r) .. "]")  
    assert(type(g) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 5, got " .. type(g) .. "]")  
    assert(type(b) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 6, got " .. type(b) .. "]")  
    assert(type(a) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 7, got " .. type(a) .. "]")  
    assert(isElement(visibleTo), "Bad argument @ 'createMarkerAttachedTo' [Expected element at argument 8, got " .. type(visibleTo) .. "]")  
    assert(type(xOffset) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 9, got " .. type(xOffset) .. "]")  
    assert(type(yOffset) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 10, got " .. type(yOffset) .. "]")  
    assert(type(zOffset) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 11, got " .. type(zOffset) .. "]") 
    local m = createMarker(0, 0, 0, mType, size, r, g, b, a, visibleTo) 
    if m then  
        if attachElements(m, element) then  
            return m  
        end  
    end  
    return false 
end 
  
  
addEvent ("objs",true) 
addEventHandler ("objs",getRootElement(),function() 
    for i,v in ipairs(mines) do 
        yx,yy,yz=unpack(v) 
        objects[i] = createObject (3929,yx,yy,yz) 
        setElementData (objects[i],"mineununtrium","true") 
        setElementData(object1s[i],"qununtrium",5) 
        marke[i] = createMarkerAttachedTo (objects[variable],"corona",4,255,255,255,50,getRootElement(),0,0,10) 
    end 
end) 
  
  
  
addEvent ("give1",true) 
addEventHandler ("give1",getRootElement(),function() 
local uut = getElementData (source,"ununtrium") 
    for i=1, #mines do 
        destroyElement[objects[i]] 
        destroyElement (marke[i]) 
        objects[i] = nil 
        marke[i] = nil 
        setElementData (source,"ununtrium",uut+3) 
    end 
end) 
  

Multi Theft Auto Player since middle of 2011.

Everybody want to act Crazy/Smart/Intelligent/Different/Unique/Innocent and Thats why I am Perfect.

Posted

It's all ok, thank you so much! But now I have another problem, the objects start losing their collisions one by one until all of this script's objects' collisions dissapear, I've restarted the script but it got worse, for what I can see, the only solution is restarting the server. What the hell is happening?

tJ5zeFm.gif

Proud owner and developer of ZNEXT: Aftermath.

Enter a post-apocalyptic San Andreas and fight over 30 types of enemies and bosses with varying difficulties and skills, improve and customize your character by leveling up, completing challenges and a solid lootbox system with no Pay-to-Win mechanics that will break your experience.

Meet new characters, creatures and weapon metas, experience an innovative combo-based melee system, or join our solid PvP modes to show other survivors who’s boss. 

Español, Pусский, Türk, عربى, Polski, Português

IP: mtasa://104.36.110.227:22003 - Discord: https://discord.gg/CxMxjvC5pB

Posted
This should do the job.
  
mines={} 
mines[1]={316.623046875, 2310.3662109375, 175.88377380371} 
mines[2]={368.55038452148, 2279.5004882812, 179.28540039062} 
mines[3]={371.35195922852, 2383.2370605469, 204.61744689941} 
mines[4]={322.07421875, 2455.6181640625, 209.29032897949} 
mines[5]={459.70449829102, 2427.0690917969, 191.66717529297} 
mines[6]={534.01818847656, 2469.5270996094, 183.58447265625} 
mines[7]={ 635.9619140625, 2403.568359375, 180.75654602051} 
mines[8]={629.318359375, 2513.05859375, 166.53332519531} 
mines[9]={ 757.48828125, 2565.681640625, 159.08197021484} 
mines[10]={881.20446777344, 2574.3151855469, 160.68569946289} 
mines[11]={778.15057373047, 2687.6740722656, 155.58869934082} 
mines[12]={958.9248046875, 2667.8623046875, 157.94715881348} 
mines[13]={912.7998046875, 2766.8828125, 182.83726501465} 
mines[14]={821.197265625, 2795.1171875, 166.05780029297} 
mines[15]={822.2978515625, 2455.7451171875, 140.25344848633} 
  
objects = {} 
marke = {} 
  
function createMarkerAttachedTo(element, mType, size, r, g, b, a, visibleTo, xOffset, yOffset, zOffset) 
    mType, size, r, g, b, a, visibleTo, xOffset, yOffset, zOffset = mType or "checkpoint", size or 4, r or 0, g or 0, b or 255, a or 255, visibleTo or getRootElement(), xOffset or 0, yOffset or 0, zOffset or 0 
    assert(isElement(element), "Bad argument @ 'createMarkerAttachedTo' [Expected element at argument 1, got " .. type(element) .. "]")  
    assert(type(mType) == "string", "Bad argument @ 'createMarkerAttachedTo' [Expected string at argument 2, got " .. type(mType) .. "]")  
    assert(type(size) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 3, got " .. type(size) .. "]")  
    assert(type(r) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 4, got " .. type(r) .. "]")  
    assert(type(g) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 5, got " .. type(g) .. "]")  
    assert(type(b) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 6, got " .. type(b) .. "]")  
    assert(type(a) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 7, got " .. type(a) .. "]")  
    assert(isElement(visibleTo), "Bad argument @ 'createMarkerAttachedTo' [Expected element at argument 8, got " .. type(visibleTo) .. "]")  
    assert(type(xOffset) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 9, got " .. type(xOffset) .. "]")  
    assert(type(yOffset) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 10, got " .. type(yOffset) .. "]")  
    assert(type(zOffset) == "number", "Bad argument @ 'createMarkerAttachedTo' [Expected number at argument 11, got " .. type(zOffset) .. "]") 
    local m = createMarker(0, 0, 0, mType, size, r, g, b, a, visibleTo) 
    if m then  
        if attachElements(m, element) then  
            return m  
        end  
    end  
    return false 
end 
  
  
addEvent ("objs",true) 
addEventHandler ("objs",getRootElement(),function() 
    for i,v in ipairs(mines) do 
        yx,yy,yz=unpack(v) 
        objects[i] = createObject (3929,yx,yy,yz) 
        setElementData (objects[i],"mineununtrium","true") 
        setElementData(object1s[i],"qununtrium",5) 
        marke[i] = createMarkerAttachedTo (objects[variable],"corona",4,255,255,255,50,getRootElement(),0,0,10) 
    end 
end) 
  
  
  
addEvent ("give1",true) 
addEventHandler ("give1",getRootElement(),function() 
local uut = getElementData (source,"ununtrium") 
    for i=1, #mines do 
        destroyElement[objects[i]] 
        destroyElement (marke[i]) 
        objects[i] = nil 
        marke[i] = nil 
        setElementData (source,"ununtrium",uut+3) 
    end 
end) 
  

Line 62 '=' expected near destroyElement, tried to fix it but it gives me other errors

tJ5zeFm.gif

Proud owner and developer of ZNEXT: Aftermath.

Enter a post-apocalyptic San Andreas and fight over 30 types of enemies and bosses with varying difficulties and skills, improve and customize your character by leveling up, completing challenges and a solid lootbox system with no Pay-to-Win mechanics that will break your experience.

Meet new characters, creatures and weapon metas, experience an innovative combo-based melee system, or join our solid PvP modes to show other survivors who’s boss. 

Español, Pусский, Türk, عربى, Polski, Português

IP: mtasa://104.36.110.227:22003 - Discord: https://discord.gg/CxMxjvC5pB

Posted

Replace

destroyElement[objects[i]] 

with

destroyElement(objects[i])  

.

Multi Theft Auto Player since middle of 2011.

Everybody want to act Crazy/Smart/Intelligent/Different/Unique/Innocent and Thats why I am Perfect.

Posted
Replace
destroyElement[objects[i]] 

with

destroyElement(objects[i])  

.

I can't test it, the objects have no collisions, I don't know why

tJ5zeFm.gif

Proud owner and developer of ZNEXT: Aftermath.

Enter a post-apocalyptic San Andreas and fight over 30 types of enemies and bosses with varying difficulties and skills, improve and customize your character by leveling up, completing challenges and a solid lootbox system with no Pay-to-Win mechanics that will break your experience.

Meet new characters, creatures and weapon metas, experience an innovative combo-based melee system, or join our solid PvP modes to show other survivors who’s boss. 

Español, Pусский, Türk, عربى, Polski, Português

IP: mtasa://104.36.110.227:22003 - Discord: https://discord.gg/CxMxjvC5pB

Posted

Replace

objects[i] = createObject (3929,yx,yy,yz) --line 49 

with

objects[i] = createObject (3929,yx,yy,yz,nil,nil,nil,false) 

If the above doesn't work (probably it should), you can try using the below function.

  
setElementCollisionsEnabled 
  

Multi Theft Auto Player since middle of 2011.

Everybody want to act Crazy/Smart/Intelligent/Different/Unique/Innocent and Thats why I am Perfect.

Posted
Replace
objects[i] = createObject (3929,yx,yy,yz) --line 49 

with

objects[i] = createObject (3929,yx,yy,yz,nil,nil,nil,false) 

If the above doesn't work (probably it should), you can try using the below function.

  
setElementCollisionsEnabled 
  

Still the same, I've tried setElementCollisionsEnabled and createObject with the nil rotation values and false lowLOD, this is so strange...

tJ5zeFm.gif

Proud owner and developer of ZNEXT: Aftermath.

Enter a post-apocalyptic San Andreas and fight over 30 types of enemies and bosses with varying difficulties and skills, improve and customize your character by leveling up, completing challenges and a solid lootbox system with no Pay-to-Win mechanics that will break your experience.

Meet new characters, creatures and weapon metas, experience an innovative combo-based melee system, or join our solid PvP modes to show other survivors who’s boss. 

Español, Pусский, Türk, عربى, Polski, Português

IP: mtasa://104.36.110.227:22003 - Discord: https://discord.gg/CxMxjvC5pB

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