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

My script adds an accessory to my local player but it doesn't attach it to the head?

Asked by 6 years ago
Edited 6 years ago
local helmet = script.Parent.Pikeman
script.Parent.MouseButton1Click:connect (function ()

playername = script.Parent.Parent.Parent.Parent.Parent.Parent.Name
player = Workspace:FindFirstChild(""..playername.."")
if player ~= nil then
        wait(0.1)
        clonedObject = helmet:Clone()
        wait(0.5)
        clonedObject.Parent = player
        player.Humanoid:AddAccessary(script.Parent.Pikeman)
        print("Found Humanoid")
        wait(0.2)


    end
end)

Any help appreciated thanks.

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Courtesy of TheGamer101 on this DevForum post.

This is because Accessory welds are currently created on the server only, never the client. This is intended right now but we might change it to make this easier to work with when using FilteringEnabled.

Here is some code you can use to add an Accessory to a humanoid locally:

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

Using addAccoutrement(character, accoutrement) is how you would add it to the character. Character being your character and accoutrement being the Accessory.

Ad

Answer this question