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
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:
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
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]