Hat Button Giver

I need help creating a button that gives any player a hat. I’ve been struggling to make it work on my own. Can anyone help me out? This is the script, but it’s not working.

local button = game.Workspace.Button

local function giveHeadHat(player)
local character = player.Character
if not character then
return
end

if character:FindFirstChild("HeadHat") then
	return
end

local hat = Instance.new("Accessory")
hat.Name = "HeadHat"
hat.Parent = character

local hatMesh = Instance.new("SpecialMesh")
hatMesh.MeshId = "rbxassetid://582169381"
hatMesh.Parent = hat

hat:AttachToCharacter(character)

end

local function onButtonClicked()
for _, player in ipairs(game.Players:GetPlayers()) do
giveHeadHat(player)
end
end

button.ClickDetector.MouseClick:Connect(onButtonClicked)

I have a question about adding hats in Roblox. Do I need to place the hat in the workspace and then manually add an attachment to it? Also, how do I position the hat on the player’s head? Do I use a rig for that? (Sorry I’m pretty new to this)

I believe accessories automatically positions and welds itself once it’s parented to a character

1 Like

I think it have to be a MeshPart not SpecialMesh.
Also you could make the accessory and put it in the serverstorage and just make the script to clone it from serverstorage and put inside the player’s character.