Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why can't I add hats to a character using a localscript?

Asked by 4 years ago
Edited 4 years ago

I tried using :AddAccessory() and :Clone() but in both cases the hat does not position itself to the characters head.

eg

workspace.SpawnPlace.Adventurer.Humanoid:AddAccessory(game.ReplicatedStorage.Character.Hair.Caster.Hair8:Clone())
0
can we see your code so far? royaltoe 5144 — 4y
0
k Edbotikx 99 — 4y
0
It works fine on the server. It's only on the client that it wont position correctly. Edbotikx 99 — 4y
0
it should be a server script anyway shouldnt it? royaltoe 5144 — 4y
View all comments (2 more)
0
No. I'm making a character customization screen. Basically all players share the same preview npc to design their character. Edbotikx 99 — 4y
0
See my answer royaltoe 5144 — 4y

1 answer

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Apparently you can't call Humanoid:AddAccessory properly from the client. Source

This is the workaround that TheGamer101 came up with: I showed an example of how to use it at the bottom, the last line of the code.

local item = local item = game.ReplicatedStorage.ClockworksShades:Clone()
item.Parent = workspace


function weldAttachments(attach1, attach2)
    local weld = Instance.new("Weld")
    weld.Part0 = attach1.Parent
    weld.Part1 = attach2.Parent
    weld.C0 = attach1.CFrame
    weld.C1 = attach2.CFrame
    weld.Parent = attach1.Parent
    return weld
end

local function buildWeld(weldName, parent, part0, part1, c0, c1)
    local weld = Instance.new("Weld")
    weld.Name = weldName
    weld.Part0 = part0
    weld.Part1 = part1
    weld.C0 = c0
    weld.C1 = c1
    weld.Parent = parent
    return weld
end

local function findFirstMatchingAttachment(model, name)
    for _, child in pairs(model:GetChildren()) do
        if child:IsA("Attachment") and child.Name == name then
            return child
        elseif not child:IsA("Accoutrement") and not child:IsA("Tool") then -- Don't look in hats or tools in the character
            local foundAttachment = findFirstMatchingAttachment(child, name)
            if foundAttachment then
                return foundAttachment
            end
        end
    end
end

function addAccoutrement(character, accoutrement)  
    accoutrement.Parent = character
    local handle = accoutrement:FindFirstChild("Handle")
    if handle then
        local accoutrementAttachment = handle:FindFirstChildOfClass("Attachment")
        if accoutrementAttachment then
            local characterAttachment = findFirstMatchingAttachment(character, accoutrementAttachment.Name)
            if characterAttachment then
                weldAttachments(characterAttachment, accoutrementAttachment)
            end
        else
            local head = character:FindFirstChild("Head")
            if head then
                local attachmentCFrame = CFrame.new(0, 0.5, 0)
                local hatCFrame = accoutrement.AttachmentPoint
                buildWeld("HeadWeld", head, head, handle, attachmentCFrame, hatCFrame)
            end
        end
    end
end


addAccoutrement(workspace.SpawnPlace.Adventurer, item)  
Ad

Answer this question