Weld script not functioning

Hey, I want to weld all the parts in my tool.

I made an if statement so that only certain parts can get welded but for some reason the scripts is trying to get welded
I’ve tried excluding the script with an if statement but it still doesn’t work

This the tool
Screen Shot 2021-07-02 at 9.42.34 PM

This is the error code

This is the welding Script:

local tool = script.Parent
local parts = tool:GetChildren()
local previous

for i = 1, #parts do
	if parts[i].className == "Part" or parts[i].className == "WedgePart" or parts[i].className == "UnionOperation" then
		if previous ~= nil then
			print(parts[i])
			local w = Instance.new("WeldConstraint")
			w.Part0 = previous
			w.Part1 = parts[i]
			w.Parent = previous
		end
	end
	previous = parts[i]
	end

I’m not sure what to do

Try this.

local tool = script.Parent
local previous = nil

for _, Part in pairs(tool:GetChildren()) do
	if not Part:IsA("BasePart") then				continue				end
	if previous then
		print(Part)
		local w = Instance.new("WeldConstraint", previous)
		w.Part0 = previous
		w.Part1 = Part
	end
	previous = Part
end

This is because previous isn’t a BasePart, put previous = parts[i] inside the 1st if statement and then it might work.

Both ya’ll solutions worked. Thank you

and union
show class name u make on this