Element is incredibly buggy

  • _Theo
    25th Jan 2018 Member 0 Permalink

    I'm currently making a new element my friend requested I add to my new mod, however it keeps saying there's an error, I've disabled all mods but Texter, Layering Helper and TPTMP, the element script looks to be normal and it works fine, I have no idea why it still gives me an error message.

     

    local psac = elements.allocate("TPT", "PSAC")
    elements.element(psac, elements.element(elements.DEFAULT_PT_SOAP))
    elements.property(psac, "Name" , "PSAC")
    elements.property(psac, "Description" , "Positronic acid, reacts violently with matter, weak acid.")
    elements.property(psac, "Color", 0xFF20BB)
    elements.property(elem.TPT_PT_PSAC, "Update", --*1
    function(i,x,y,s,nt) --1
    if s ~=8 and nt ~=0 and nt - s > 0 then --2
    if math.random(1,100) == 10 then --3
    for r in sim.neighbors(x,y,1,1) do --4
    if sim.partProperty(r, "type") ~= elements.DEFAULT_PT_DMND then --5
    if sim.partProperty(r, "type") ~= elements.DEFAULT_PT_VACU then --6
    if sim.partProperty(r, "type") ~= elements.DEFAULT_PT_VOID then --7
    if math.random(1,6) == 3 then --8
    sim.partKill(r)
    sim.partProperty(i, "temp", sim.partProperty(i, "temp")+1600) --It says there's an error at this line, i've used this peice of code with a different temp on a different element above and it doesn't give me an error there.
    if math.random(1,25) == 20 then --9
    sim.partKill(i)
    end --9
    end --8
    end --7
    end --6
    end --5
    end --4
    end --3
    end --2
    end --1
    ) --*1

    Edited once by TuDoR2007. Last: 25th Jan 2018
  • ssccsscc
    25th Jan 2018 Member 0 Permalink

    for r in sim.neighbors(x,y,1,1) do --4 causing the problem because inside it parts can kill self in this line
    sim.partKill(i)

    But after part got killed cycle is continues and as result next iteration trying to access already deleted element id


    To fix this you can add "return" after line "sim.partKill(i)"
    or place lines:

    if math.random(1,25) == 20 then --9
    sim.partKill(i)
    end --9

    after for cycle.

    Edited 3 times by ssccsscc. Last: 25th Jan 2018
  • _Theo
    25th Jan 2018 Member 0 Permalink

    Thanks, I'm adding this to my mod now.

  • jacob1
    25th Jan 2018 Developer 0 Permalink
    Also why do you put comments on every conditional? I recommend using proper indentation to let you easily see which if statement goes to what. Spacing works on the forum using <code> tags
  • _Theo
    27th Jan 2018 Member 0 Permalink

    I use both, but since i just copy-pasted the code from my script it wouldn't work, I knew there was some sort of box thing to put code in but i forgot how to make it.