Not all instances from table being removed

Hey everyone. This script isn’t removing everything from the table. If you can help me point out what’s wrong with it, that’d be greatly appreciated.

Quick Note: “Spawns” is the table.

for i, Positions in Spawns do
		if Positions ~= nil then
		    table.remove(Spawns, i)
			print("Positions removed from table: "..tostring(Positions.Name..i))
		else
			print("Nothing to remove from table.")
		end
	end
1 Like

Use table.clear or below post setting the value at each index to nil.

Using table.remove is bad as it shifts the remaining index down by one for example removing a value in index 2 makes, i=3 goes to i=2, then the iteration goes to 3 and skips the value that replaced the one at index 2.

Havent checked this out you might need to print the index, value, and table to find out what is happening in each iteration.

Ah, I see! Thanks for that! I’ll try my best to remember this in the future!

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