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)