Whats wrong with that algorithm?

I need call sound for each object directly, but this code dont work for some reason

Slave = class()
--physics.pause()
function Slave:init(x)
    local vecs = {vec2(0,0),vec2(0,130),vec2(30,130),vec2(0,0),vec2(30,0),vec2(30,130)}
    
    self.skull = physics.body(CIRCLE,5)
    self.skull.x = WIDTH/2
    self.skull.y = 60
    

    
    self.slaves = {}
    
    self.time = 0
    -- you can accept and set parameters here
    
 --table to random time values, which will use below
    self.jump = {}
    
    for i = 1,5 do
    self.slave = physics.body(POLYGON,unpack(vecs))
    table.insert(self.slaves,self.slave)
--insert random values to make this guys jump
    table.insert(self.jump,math.random(2,6))
    self.slave.x = math.random(WIDTH/2-50,WIDTH/2+50)
    self.slave.y = math.random(HEIGHT/2)
    end
    
    
    self.wallB = physics.body(EDGE,vec2(0,59),vec2(WIDTH,59))
    self.wallB.restitution = 0.1
    
    self.wallL = physics.body(EDGE,vec2(291,539),vec2(352,65))
    self.wallL.restitution = 0.1
    
    self.wallR = physics.body(EDGE,vec2(681,65),vec2(738,539))
    self.wallL.restitution = 0.1
end

function Slave:draw()
    -- Codea does not automatically call this method
    self.time = self.time + DeltaTime
    local time = math.floor(self.time) 
    local jump ={}
    for i = 1,5 do
        table.insert(jump,"SOUND_JUMP, 1")
    end
    
    
    
    for k,v in pairs(self.slaves) do
        for a,b in pairs(self.jump) do 
--here, this string makes random jumps for each man
    if math.fmod(time,b) == 0 and a == k then
        v.linearVelocity = v.linearVelocity + vec2(0,8)

--and here sound didnt appear, but must
        sound(jump[a])
    end
    end
    pushMatrix()
    translate(v.x,v.y)
    rotate(v.angle)
    sprite("Documents:1111slave"..k,10,80,512/3,512/3)
    popMatrix()
    end
    
    pushMatrix()
    translate(self.skull.x,self.skull.y)
    rotate(self.skull.angle)
    sprite("Documents:1111Skull",0,0,512/17,512/17)
    popMatrix()
end

function Slave:touched(touch)
    -- Codea does not automatically call this method
end

you are not populating the self.slaves table. you have the for i=1, 5 do loop, but each time you just set the same self.slave. Each line inside that loop should start with self.slaves[i]=

@yojimbo2000 thank you for respond, but i changed that loop (and few strings below) and nothing happends, except speeding up my fps rate :

for i = 1,5 do
    self.slave = physics.body(POLYGON,unpack(vecs))
    self.slaves[i] = self.slave
    table.insert(self.jump,math.random(2,6))
    self.slave.x = math.random(WIDTH/2-50,WIDTH/2+50)
    self.slave.y = math.random(HEIGHT/2)
    end

And i changed this :

    self.time = self.time + DeltaTime
    local time = math.floor(self.time) 
    local jump ={}
    for i = 1,5 do
        table.insert(jump,"SOUND_JUMP, 1")
    end
    
    
    
        for a,b in pairs(self.jump) do 
    if math.fmod(time,b) == 0 then
        self.slaves[a].linearVelocity = self.slaves[a].linearVelocity + vec2(0,8)
        sound(jump[a])
    end
    pushMatrix()
    translate(self.slaves[a].x,self.slaves[a].y)
    rotate(self.slaves[a].angle)
    sprite("Documents:1111slave"..a,10,80,512/3,512/3)
    popMatrix()
    end
    

P.s and if i take self.slaves out of loop, i will not have several physics bodies, only one

@lupino8211 Why are you creating a table for SOUND_JUMP when it’s the same sound for all 5 entries. Forget about the sound table and just do the sound when you need to.

@lupino8211 If you still want to create a table for sounds, here’s how.


function setup()
    local jump ={}
    for i = 1,5 do
    -- create the sound table
        table.insert(jump,SOUND_JUMP)
    end
    
    -- play the sound
    a=3
    sound(jump[a],1)
end

@dave1707 now i see, what i dont need table to play sound, but i want play this sound once, then one of the bodyes jumping

self.slaves[a].linearVelocity = self.slaves[a].linearVelocity + vec2(0,8)

but if i let only

sound(SOUND_JUMP,1)

it would make wild noise