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.
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
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
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)
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)
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.
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