Removing single objects from an array through collision

I was just wondering is it possible to remove an object from an array through collision this is what I have so far but through collision it deletes half of the objects from the array/table

local function chomp(event)
if event.phase == “ended” then
if event.object2 == head then
print(#allPeople… " number before")

for i = 1, #allPeople do
local onePeople = allPeople[i]
if (onePeople and onePeeps.x) then
onePeople :removeSelf()
table.remove( allPeople, i )
end
end
print(#allPeople… " number after")
updateScore()
xpUp()
timer.performWithDelay(2000, delaySpawn, 1)
end
end
end

Runtime:addEventListener(“collision”, chomp) [import]uid: 87730 topic_id: 36176 reply_id: 336176[/import]

to now add on to my dilemma I have somewhat fixed the deletion of objects through collision, what I wish to accomplish now is per object deleted spawn 2 more. Once doing so 1 of the two are eadible the other is free to roam with physics attached and is not eadible [import]uid: 87730 topic_id: 36176 reply_id: 143672[/import]

One common mistake I see here is that you need to go through the list backwards, not forwards.
Imagine you have a list: { “a”, “b”, “c” }.
For whatever reason you delete index 1, IE the “a” - your list is now { “b”, “c” }.
But, next time you go through the loop, index 2, that will be “c”, you’ve skipped “b” entirely.

So instead of

for i = 1, #allPeople do

do

for i = #allPeople, 1, -1 do

That’ll certainly help. [import]uid: 46639 topic_id: 36176 reply_id: 143685[/import]

thank you for the speedy reply there rakoonic, however since a re-occurrence and battling of errors I moved to a more “prominent” system provided by danny for spawning objects the right way changing the entire feel for the code, I’ve moved onto complexity to even more complexity.

If you are familiar with the tutorial of Danny it is spawning an object with the use of parameters (very effective means) but now removing said objects on a collision event seems to be a commonly unanswered question I’ve yet to find the answer to [import]uid: 87730 topic_id: 36176 reply_id: 143739[/import]

to now add on to my dilemma I have somewhat fixed the deletion of objects through collision, what I wish to accomplish now is per object deleted spawn 2 more. Once doing so 1 of the two are eadible the other is free to roam with physics attached and is not eadible [import]uid: 87730 topic_id: 36176 reply_id: 143672[/import]

One common mistake I see here is that you need to go through the list backwards, not forwards.
Imagine you have a list: { “a”, “b”, “c” }.
For whatever reason you delete index 1, IE the “a” - your list is now { “b”, “c” }.
But, next time you go through the loop, index 2, that will be “c”, you’ve skipped “b” entirely.

So instead of

for i = 1, #allPeople do

do

for i = #allPeople, 1, -1 do

That’ll certainly help. [import]uid: 46639 topic_id: 36176 reply_id: 143685[/import]

thank you for the speedy reply there rakoonic, however since a re-occurrence and battling of errors I moved to a more “prominent” system provided by danny for spawning objects the right way changing the entire feel for the code, I’ve moved onto complexity to even more complexity.

If you are familiar with the tutorial of Danny it is spawning an object with the use of parameters (very effective means) but now removing said objects on a collision event seems to be a commonly unanswered question I’ve yet to find the answer to [import]uid: 87730 topic_id: 36176 reply_id: 143739[/import]

to now add on to my dilemma I have somewhat fixed the deletion of objects through collision, what I wish to accomplish now is per object deleted spawn 2 more. Once doing so 1 of the two are eadible the other is free to roam with physics attached and is not eadible [import]uid: 87730 topic_id: 36176 reply_id: 143672[/import]

One common mistake I see here is that you need to go through the list backwards, not forwards.
Imagine you have a list: { “a”, “b”, “c” }.
For whatever reason you delete index 1, IE the “a” - your list is now { “b”, “c” }.
But, next time you go through the loop, index 2, that will be “c”, you’ve skipped “b” entirely.

So instead of

for i = 1, #allPeople do

do

for i = #allPeople, 1, -1 do

That’ll certainly help. [import]uid: 46639 topic_id: 36176 reply_id: 143685[/import]

thank you for the speedy reply there rakoonic, however since a re-occurrence and battling of errors I moved to a more “prominent” system provided by danny for spawning objects the right way changing the entire feel for the code, I’ve moved onto complexity to even more complexity.

If you are familiar with the tutorial of Danny it is spawning an object with the use of parameters (very effective means) but now removing said objects on a collision event seems to be a commonly unanswered question I’ve yet to find the answer to [import]uid: 87730 topic_id: 36176 reply_id: 143739[/import]

to now add on to my dilemma I have somewhat fixed the deletion of objects through collision, what I wish to accomplish now is per object deleted spawn 2 more. Once doing so 1 of the two are eadible the other is free to roam with physics attached and is not eadible [import]uid: 87730 topic_id: 36176 reply_id: 143672[/import]

One common mistake I see here is that you need to go through the list backwards, not forwards.
Imagine you have a list: { “a”, “b”, “c” }.
For whatever reason you delete index 1, IE the “a” - your list is now { “b”, “c” }.
But, next time you go through the loop, index 2, that will be “c”, you’ve skipped “b” entirely.

So instead of

for i = 1, #allPeople do

do

for i = #allPeople, 1, -1 do

That’ll certainly help. [import]uid: 46639 topic_id: 36176 reply_id: 143685[/import]

thank you for the speedy reply there rakoonic, however since a re-occurrence and battling of errors I moved to a more “prominent” system provided by danny for spawning objects the right way changing the entire feel for the code, I’ve moved onto complexity to even more complexity.

If you are familiar with the tutorial of Danny it is spawning an object with the use of parameters (very effective means) but now removing said objects on a collision event seems to be a commonly unanswered question I’ve yet to find the answer to [import]uid: 87730 topic_id: 36176 reply_id: 143739[/import]