A "technical" problem in my script

I don’t know how I can possibly explain this as my head is hurting now for the time I spent trying to find out why a table is looping the same value twice some reason(s) I couldn’t find.
Basically, we got a table of three parts, we also got another table with other three tables. Each part will clone one of the table’s properties.

 for _,tab in pairs(workspace.SR["Voting Block"].Voting.Touching:GetChildren()) do
		print("*****")
		for _,tab2 in pairs(extra) do
			print("-")
			if tab:FindFirstChild("Value") then tab["Value"]:Destroy() end
			local newval = Instance.new("StringValue",tab)
			newval.Value = tab2.Name
			Instance.new("NumberValue",newval).Value = tab2.Image
			Instance.new("ObjectValue",newval).Value = tab2.Path
			for tab4,tab3 in pairs(extra) do
				if tab3 == tab2 then
					table.remove(extra,tab4)
				end
			end
		end	
	end

As you can see here, we are cloning each table to the part values, then removing it from the main table so we don’t loop in it again. Here’s where the problem occurs:
image
In the first loop, it loops twice in the main table somehow, which means it removed the two tables from the main table, the second time it found a table and worked as intended, but the third time it couldn’t continue as the table is empty.
Any suggestions would be very helpful, my brain is dead at this point ladies and gentlemen.

Your variable names are killing me lol :slight_smile: It’s really helpful if you name your index/value variables after what they represent rather than “tab, tab2, tab3, tab4”.

Anyways, I don’t understand this code. What is extra supposed to be?

Also, removing elements from a table while you’re iterating it is generally a bad idea. You may or may not skip elements. Extending discussion here.

2 Likes

Was about to say the same thing, well played.

Adding on, an idea when looping is to make a copy of the table so that any actions performed on the table don’t have an impact on the original. That way you don’t have any issues with missing values.

1 Like

Explaining lesson with b5x0! Get 3 lessons for free today.

I have the habit of renaming things randomly and remember them somehow after a while.
tab: The part we are putting the values on.
tab2: The table that we are using then removing.
tab3: The value of the table we are looping to remove tab2.
tab4: The index of the table we are looping to remove tab2.
extra: The table that contains all the tab2 tables.

1 Like

extra is a clone table of another cloned table of the original table. Yeah a complicated thing :person_shrugging:

This is because table.remove() shifts the table to remove the empty space after you remove it. This can mess up iteration and make you skip elements/reach a certain element twice.

To fix this, just set it to nil instead of using table.remove():

table.remove(extra, tab4)
-- instead of above, do this:
extra[tab4] = nil

Since this method doesn’t shift the table, you don’t have to worry about messing up iteration.

If you actually need to shift the table, you can still use table.remove(), but you have to iterate through the table backwards instead. However, this method is slower and it’s better to just set it to nil.

1 Like