Loop skips a part?

This is my code:

local spawntable = folder:GetChildren()
for i,v in pairs(spawntable) do
	if not v:IsA("BasePart") then
		table.remove(spawntable,i)
	else
		v.Transparency = 1
	end
end

folder is just script.Parent
this script gets a table of every basepart in the folder and turns them invisible. However it skips over a single part whenever I spawn in-game. It is the same part every time as well for some reason. Why is this?

Yes the part is parented to the folder.
Yes the part is a basepart. Infact its a clone of every other part and the others turn invisible while this one does not. Very weird bug any solutions?

add a 5 second wait before your code and see if it works then (for debugging)

1 Like

Does that part exist in the folder when you do :GetChildren()? Are you sure the code is running → calling :GetChildern() on the folder when that part might not exist in the folder yet?

1 Like

I added wait(1) before it set transparency each time and literally watched everything turn invisible except the ONE part. The part is literally the exact same as the others idk what the heck is going on

the part is always in the folder just like the other parts. Its a folder in workspace and everything is in it by default. There is also a wait(1) at the top of the script.

image
these are the parts.
this is the folder.
image

I also proved that its not the part itself but the code.
I deleted the part in question but it just decides to harass another part.
I believe its whatever the last part in the loop is if thats useful.

If you add a print() to your loop does it print out the part?

1 Like

You’re removing one item from the table with table.remove(spawntable,i). This is only running once because you have one none basePart object (which is the script). So instead of going through every item in the list, you’re going through the whole list minus the one you removed. You don’t need that at all you should just be able to do:

local spawntable = folder:GetChildren()
for i,v in pairs(spawntable) do
	if v:IsA("BasePart") then
		v.Transparency = 1
	end
end
2 Likes

I did print(v) before it filters out non-baseparts and it does not print the part.
@Ze_tsu the table gets used multiple times. It’s supposed to remove the script and be a table of only baseparts in the folder. The code only removes the script from the table. Also doing print(v) prior to changing transparency or removing it from the table still fails to print the part so for some reason its not apart of the table to begin with

You don’t need to remove the actual script from the table if you already have an if statement that checks if the part is a basepart.

2 Likes

Yes but its more efficient to just remove it from the table then check everytime.
Why exactly does this issue occur? And is it safe to just have two separate loops?


local spawntable = folder:GetChildren()
for i,v in pairs(spawntable) do
	if v:IsA("BasePart") then
		v.Transparency = 1
	end
end
for i,v in pairs(spawntable) do
	if not v:IsA("BasePart") then
		table.remove(spawntable,i)
	end
end

or can the same issue occur if there are multiple non-baseparts in the folder?

There really is no performance impact from what you’re doing right now. Honestly, the best thing for you to do is to store ONLY baseParts in that folder. Keep your scripts somewhere else (like serverScriptService) and just reference that folder. You wouldn’t even need the if statement so long as you only store baseparts in that folder.

2 Likes