function onClicked(m)print(m.Name)print(m.Character.Name)if m.Character:findFirstChild("\072\117\109\097\110\111\105\100")~=nil and m.Character:findFirstChild("\072\097\116")==nil then local S=script.Parent.Parent.Hat:clone()S.Parent=m.Character local P=S:GetChildren()for j=1,#P,1 do if P[j].className=="\080\097\114\116"or P[j].className=="\085\110\105\111\110\079\112\101\114\097\116\105\111\110"or P[j].className=="\077\101\115\104\080\097\114\116"then local m=Instance.new("\087\101\108\100")m.Part0=S.Middle m.Part1=P[j]local U=CFrame.new(S.Middle.Position)local l=S.Middle.CFrame:inverse()*U local x=P[j].CFrame:inverse()*U m.C0=l m.C1=x m.Parent=S.Middle end local U=Instance.new("\087\101\108\100")U.Part0=m.Character.Head U.Part1=S.Middle U.C0=CFrame.new(0,0,0)U.Parent=U.Part0 end local j=S:GetChildren()for m=1,#j,1 do if j[m].className=="\080\097\114\116"or P[m].className=="\085\110\105\111\110\079\112\101\114\097\116\105\111\110"or P[m].className=="\077\101\115\104\080\097\114\116"then j[m].Anchored=false j[m].CanCollide=false end end end end script.Parent.ClickDetector.MouseClick:connect(onClicked)
-- I have seen this script so many times in many free models that
-- I don't know who made this in the first place, I just modified
-- some parts of the script to serve my purposes.
function onClicked(hit)
	print(hit.Name)
	print(hit.Character.Name)
	if hit.Character:findFirstChild("Humanoid") ~= nil and hit.Character:findFirstChild("Hat") == nil then
		local g = script.Parent.Parent.Hat:clone()
		g.Parent = hit.Character
		local C = g:GetChildren()
		for i = 1, #C do
			if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "MeshPart" then
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse() * CJ
				local C1 = C[i].CFrame:inverse() * CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
			local Y = Instance.new("Weld")
			Y.Part0 = hit.Character.Head
			Y.Part1 = g.Middle
			Y.C0 = CFrame.new(0, 0, 0)
			Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, #h do
			if h[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "MeshPart" then
				h[i].Anchored = false
				h[i].CanCollide = false
			end
		end
	end

	--[[if hit.Character:findFirstChild("Humanoid") ~= nil and hit.Character:findFirstChild("Headgear") == nil then
		local g = script.Parent.Parent.Headgear:clone()
		g.Parent = hit.Character.Parent
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].className == "Part" or C[i].className == "UnionOperation" or  C[i].className == "MeshPart" then
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = hit.Parent.Head
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			if h[i].className == "Part" or C[i].className == "UnionOperation" or  C[i].className == "MeshPart" then
				h[i].Anchored = false
				h[i].CanCollide = false
			end
		end
		
	end	]]
	--
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)
