Help Removing Part From Table

I have a script, everything works, except for one thing.

When i ask to remove a part from a table it doesnt work? no error or anything.

–Heres A Snippet Of The Script

local partsOption1 = {}

	while task.wait(0.1) do
		if Build1 then
			for i, part in pairs(Build1:GetChildren()) do
				if part:isA("BasePart") and part.Name ~= "WeldPart" then
					if part:FindFirstChild("WeldConstraint") then
						if partsOption1 and weldOption1 then
							for i, number in pairs(partsOption1) do
								local part2 = partsOption1[i]
								print(part2)
								local distance = (part2.Parent.PrimaryPart.Position - part.Position).Magnitude
								print(distance)
								if distance < 5 and part.Name == part.Name then
									part.Transparency = 0
									part.CanCollide = true
									part2:Destroy()
									table.remove(partsOption1, i)
									weldOption1:Destroy()
									weldOption1 = nil
									if #PartStoreOption1 == 0 then
										MovingTruckProp:Destroy()
										CurrentlyBuild = false
										Constructing = false
										ToolValue = 0
										weldOption1 = nil
										MovingTruckProp = nil
										break
									else
										print("still good")
									end
								end
							end
						end
					end
				end
			end
		end
	end
end
4 Likes

does any of your prints actually print out ?

5 Likes

Did you try setting the index inside of the table to nil? Not sure if you are also trying to destroy this part within the table (assuming it is an instance), but you could try doing

 partsOption1[i] = nil    

If you are trying to destroy the part, you can add following line before setting the index of partOption1 to nil:

partsOption1[i]:Destroy()
4 Likes

whoops i kinda had some random prints from earlier

4 Likes

even if parts are removed from the table it will not work correctly because you are removing elements from the table while iterating over it, so it will end up not iterating over some of the elements.
example:

x={1,2,3,4,5,6,7,8}
for i,v in pairs(x) do 
    if v>1 then
        table.remove(x,i)
    end
end
print(x)

output: {1,3,5,7}

You can prevent this by iterating over the table backwards with a for loop

2 Likes

could you show me how i couild implement that into my script?

1 Like

in the case of the example script you could do it like this instead:

x={1,2,3,4,5,6,7,8}
for i = #x,1,-1 do 
    if x[i]>1 then
        table.remove(x,i)
    end
end
3 Likes

i know this might be kinda annoying but where in the script should i implement it?

2 Likes

instead of iterating over the table like this you do it with the backwards for loop, and replace any uses of the variable “number” with partsOption1[i]

1 Like

try not to use so many nested statements because i cant rlly read the script

3 Likes

this is how you can remove the part from the table

local partsOption1 = {}
-- add part
table.remove(partsOption1,table.find(partsOption1,i))
1 Like

thanks for the help! got it figured out

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