Jared Fogle the subway guy HACKED INTO MY GAME

Jared Fogle the subway guy HACKED INTO MY GAME

1 Like

Instead of “i” it should be a number which is in the order the names are set out. This will remove “Doofus”:

table.remove(x, 2)
1 Like

That’s not the point. I know how to remove doofus.

The problem is that the loop skips over it in my code. I’m assuming its a problem with table.remove

Thing is, I dont remember this happening before. I’m just really confused.

1 Like

The reason this is happening is because when you use table.remove, it shifts the entire table down by 1 like explained:
image

So your loop indexes incorrectly because you expect that the table length is going to stay constant.

For example, lets say you have your table of:

{1, 2, 3, 4}

And your code is:

for index, number in ipairs(number_table) do
	if number ~= 2 then
		table.remove(number_table, index)
	end
end

On the first loop, it removes the 1 and after the removal, your table length is 3, your loop index has been increased to 2.

Table now looks like this:

{2, 3, 4}

The problem occurs here, because now, on the next loop when we check for the value of the number, it actually returns 3, because our loop index is 2. Since it detects 3 on the second index, it removes the second index.

Table now looks like this:

{2, 4}

On our last loop, our index starts at 3, which is bigger than the size of our current table, so it ends the loop, therefore ending with the above table.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.