--Sam


local rap = script.Parent.RightArmParts:GetChildren()
local lap = script.Parent.LeftArmParts:GetChildren()
local rlp = script.Parent.RightLegParts:GetChildren()
local llp = script.Parent.LeftLegParts:GetChildren()
local hp = script.Parent.HeadParts:GetChildren()
local tp = script.Parent.TorsoParts:GetChildren()

function WELDY(table, weldTo)
	local w2 = Instance.new("Weld")
	w2.Parent = script.Parent.Torso
	w2.Part0 = w2.Parent
	w2.C0 = CFrame.new()
	w2.Part1 = script.Parent.Head
	w2.C1 = CFrame.new(0, -1.5, 0)
	for i = 1, #table do 
		if table[i]:IsA("BasePart") then
			local w = Instance.new("Weld")
			w.Parent = weldTo
			w.Part0 = w.Parent
			w.C0 = weldTo.CFrame:inverse()
			w.Part1 = table[i]
			w.C1 = table[i].CFrame:inverse()
			w.Part1.Anchored = false
		end	
	end
	weldTo.Anchored = false
	for i = 1, #table do
		if table[i]:IsA("BasePart") then
			table[i].Anchored = false
		end
	end
	weldTo.Anchored = false
end 

function CFRAMEIT(table, weldTo)
	for i = 1, #table do
		if table[i]:IsA("BasePart") then
			local cf = table[i].CFrame
			local v = Instance.new("CFrameValue")
			v.Name = "CF"
			v.Value = cf
			v.Parent = table[i]
		end
	end
	local v = Instance.new("CFrameValue")
	v.Name = "CF"
	v.Value = weldTo.CFrame
	v.Parent = weldTo
end

function WELDIT(table, weldTo)
	if not weldTo:findFirstChild("CF") then
		WELDY(table, weldTo)
	elseif weldTo:findFirstChild("CF") then
		for i = 1, #table do
			if table[i]:IsA("BasePart") then
				local w = Instance.new("Weld")
				w.Parent = weldTo
				w.Part0 = w.Parent
				w.C0 = weldTo.CF.Value:inverse()
				w.Part1 = table[i]
				w.C1 = table[i].CF.Value:inverse()
				table[i].Anchored = false
			end
		end
		weldTo.Anchored = false
	end
end

function weld(t, weldTo)
WELDIT(t, weldTo)
end 

weld(rap, script.Parent["Right Arm"])
weld(lap, script.Parent["Left Arm"])
weld(rlp, script.Parent["Right Leg"])
weld(llp, script.Parent["Left Leg"])
weld(tp, script.Parent["Torso"])
weld(hp, script.Parent["Head"])
