I had a script that adds a custom nametag. And then there was a problem that whenever the user had a hat, it would cover the nametag. So I modified it that if there was a hat, it's parent would be the hat, and it would no longer be covered by the hat. The following script doesnt seem to work and doesnt give any errors.

The following is a normal script that is triggered when a Player is added.

wait(1)
	local doeshavehat = true
	local User = newPlayer.Name
	local nametagclone = game.ServerStorage.NameTag:Clone()
	for i,v in pairs(newPlayer.Character:GetChildren()) do
		if v:IsA("Hat") then
			nametagclone.Parent = newPlayer.Character[v.Name]
			newPlayer.Character[v.Name].NameTag.NAME.Text = newPlayer.Name
			break
		else
			end
	end
	if doeshavehat == false then
	nametagclone.Parent = newPlayer.Character.Head
	newPlayer.Character.Head.NameTag.NAME.Text = newPlayer.Name
	else
		end
	game.Workspace[User].Humanoid.DisplayDistanceType = "None"