--[[ This weld script is cool!
	It will use the structure of models to weld. It treats each model as a separate moving body
	except if name a part "AHandle" and insert a BoolValue named "WeldToRoot" into it.
--]]


wait(1) --thanks PSG

local AvoidWelding = {--add models you want this script to not weld in here
	["Coupler"]=true
}

function weld(i,b)
	local weld1 = Instance.new("Weld") 
	weld1.Name = b.Name
	weld1.Part0 = i
	weld1.Part1 = b
	weld1.C1 = b.CFrame:inverse()*i.CFrame
	weld1.Parent = i
end

function welding(p,ph)
	p:MakeJoints()
	local handle = p:FindFirstChild("AHandle")
	if handle and ph and handle:FindFirstChild("WeldToRoot") then
		weld(handle,ph)
	end
	if not handle then
		for _,j in pairs (p:GetChildren()) do
			if j:IsA("BasePart") then
				handle = j
				break
			end
		end
	end
	if not handle then
		handle = ph
	end
	for _,j in pairs (p:GetChildren()) do
		if j:IsA("BasePart") then
			weld(handle,j)
		elseif j:IsA("Model") and not AvoidWelding[j.Name] then
			welding(j,handle)
		end
	end
end

function unanchoring(p)
	for _,j in pairs (p:GetChildren()) do
		if j:IsA("BasePart") then
			j.Anchored = false
		elseif j:IsA("Model") then
			unanchoring(j)
		end
	end
end

wait(1)
welding(script.Parent)
wait(0.5)
unanchoring(script.Parent)
