Hat not attaching to player head

Hello, recently i have made a script that when you click a button it clones a hat from replicated storage to players head but im having a problem when it was supposed to attach players head. Someone pls help me solve this. It spawns mid air in the middle of the map


image

script:
local button = script.Parent
local hatName = script.Parent.Parent.Name
local player = game.Players.LocalPlayer

button.MouseButton1Click:Connect(function()
local hat = game.ReplicatedStorage:WaitForChild(“Hats”):FindFirstChild(hatName)

if hat then
	local clonedHat = hat:Clone()
	local character = player.Character or player.CharacterAdded:Wait()

	for _, accessory in pairs(character:GetChildren()) do
		if accessory:IsA("Accessory") and accessory.Name == hatName then
			accessory:Destroy()
		end
	end

	clonedHat.Parent = character
end

end)

1 Like

You didnt set the cframe of the hat and forgot to attach it to player’s head

an accessory is supposed to do all that for you

is the handle anchored?
also, no point using pairs, luau does iteration from a table for you

then He parented it to the wrong place

no, he parented it to the character, which is where an accessory should go

have you ever worked with accessories before

no i didnt work with accessories

then why are you chatting here

Trying to help with the problem

well you have no expertise in this issue, and so far all you’ve done is provided wrong answers

what is the problem then in your opinion

either the hat is anchored which prevents a weld from working or the hat attachment is done wrong

Its not, when i anchor it spawns mid air but doesnt fall. I think the problem might be the attachment