Problem with welding

Alright, so, I have a problem with welding, when I touch the hat giver it should weld the hat to my head, but it instead welds me below the map and the hat doesn’t go anywhere (That means it is still in the same position) and I have no idea how to fix it.

Script:

local debounce = false
local hat = script.Parent.Parent.Hat
local d = hat:GetChildren()

for i = 1, #d do
	if d[i]:IsA("BasePart") then
		local newweld = Instance.new("Weld")
		newweld.Part0 = hat.PrimaryPart
		newweld.Part1 = d[i]
		newweld.C0 = hat.PrimaryPart.CFrame:Inverse()
		newweld.C1 = d[i].CFrame:Inverse() 
		newweld.Parent = d[i]
	end
end



local function weld(a, b) -- a = CurrentPart, b = Head
 local weld = Instance.new("Weld")
	
	weld.Part0 = b
	weld.Part1 = a
	weld.C0 = b.CFrame:Inverse() * a.CFrame
	weld.Parent = hat.PrimaryPart  
	

end


script.Parent.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum then
			local char = hum.Parent
			hat:SetPrimaryPartCFrame(char:WaitForChild("Head").CFrame)
			for _, part in pairs(d) do
				if part:IsA("BasePart") or part:IsA("MeshPart") or part:IsA("UnionOperation") then		
					weld(part, char:WaitForChild("Head"))
					part.Anchored = false
					part.CanCollide = false	
				end
			end
		end
	end
	wait(10)
	debounce = false
end)```

Did you unanchor the hat? If you didn’t, try unanchoring it.

I did, look where I called the weld() function, below that function I un-anchored the part

1 Like

Nevermind, I fixed the problem, the problem was that there was a weld that welded the baseplate to the hat so it welded me below the map.