Attempting to allow players to give themselves a hat

    player.Chatted:connect(function(msg)
        if (msg:lower() == "/e hat") then
            if (player.Character:FindFirstChild("Humanoid")) then
            local Copy = game.ReplicatedStorage:FindFirstChild("Hat1"):Clone()
            local player = game.Players:GetPlayerFromCharacter(player)
            Copy.Parent = player.Character
            end
        end
    end)
end)

I’m unsure what I am doing incorrectly, any advice would be appreciated. Thank you.

I am not sure of the problem here, but what I know is that you don’t have to define the player twice.

1 Like

A bad habit I fall into far too often.

Are you using Hat or Accessory?
If you use Hat, stop using it. It is a deprecated instance that has garbage functionality.

EDIT: Also, shorten your code when you can, as long as it is still readable.

local Copy = game.ReplicatedStorage:FindFirstChild("Hat1"):Clone()
local player = game.Players:GetPlayerFromCharacter(player)
Copy.Parent = player.Character

could be replaced with

game.ReplicatedStorage:FindFirstChild("Hat1"):Clone().Parent = player.Character
2 Likes

I am using an accessory. I’ve been trying to resolve the issue however I’m still not sure why it won’t work.

Does the accessory have a Handle part?
Do you have any errors in the output?

1 Like

To add an accessory/hat to a character, use the function AddAccessory inside of Humanoid, it works with both types since Hat is a descendant of Accessory and simulates the same behaviour

Character.Humanoid:AddAccessory(accessory)

2 Likes

No errors appear to show, and it has a handle.

I attempted something like this but still nothing.

   local humanoid = character:WaitForChild("Humanoid")
     player.CharacterAppearanceLoaded:connect(function(character)
 player.Chatted:connect(function(msg)
        if (msg:lower() == "/e hat") then
            if (player.Character:FindFirstChild("Humanoid")) then
            Humanoid:AddAccessory(game.ReplicatedStorage.Hat1) 
            end
        end
    end)
end) 

Can you show us what happens, the properties of the accessory, and the child-parent structure of the accessory?

1 Like

The problem there is that your code is not structured correctly.
You are defining a Humanoid BEFORE the event if fired.

Your character and humanoid variable definition should be inside the event, not outside it

Player.Chatted:Connect(function(msg)
   local character = Player.Character
   if not character then return end

   Humanoid = character.Humanoid
   Humanoid:AddAccessory()
end)
1 Like

The accessory itself is not the issue. I have successfully been able to obtain the hat through different means (spawning with it on). I am just having trouble implementing it through a command system.
image

I’m still unsure what I am doing incorrectly.

   local humanoid = character:WaitForChild("Humanoid")
     player.CharacterAppearanceLoaded:connect(function(character)
Player.Chatted:Connect(function(msg)
 if (msg:lower() == "/e hat") then
   local character = Player.Character
   if not character then return end
      Humanoid = character.Humanoid
      Humanoid:AddAccessory(game.ReplicatedStorage.Hat1) 
   end
end)

Ok here’s the two things that’s wrong with your code

1. Chatted only need to be binded once, not every time the character is added
2. My original code from that post included an error, it has been fixed in the post

1 Like

I think I will call it a night. I’m just unable to do it correctly. Thank you for taking the time to respond to my thread.

That’s probably your issue. Most likely the variable isn’t garbage collected when you respawn, so it produces no error.