Roblox recently added Folders to developers' play. They are like Models, except the Parts included won't move together when you interact with them in Edit mode (Roblox Studio), making grouping much easier.
So the Parts you want welded can get placed in a Folder, and the script will only return the children of said Folder. This is technically a whilelist, but it will do the job.
Place this script wherever you want, just make sure the scan() function targets the Folder.
while (not script.Parent.Cloned.Value) do wait() end
local parts = {}
function scan(p)
for _,v in pairs(p:GetChildren()) do
if (v:IsA("BasePart")) then
table.insert(parts,v)
end
scan()
end
end
function weld()
scan(workspace.ToWeld)
local prev
for _,v in pairs(parts) do
if (prev) then
local weld = Instance.new("Weld")
weld.Part0 = prev
weld.Part1 = v
weld.C0 = prev.CFrame:inverse()
weld.C1 = v.CFrame:inverse()
weld.Parent = prev
end
prev = v
end
wait(1)
for _,v in pairs(parts) do v.Anchored = false end
script.Parent.Welded.Value = true
end
weld()