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

How to Change A Characters Hair Using a LocalScript?

Asked by 4 years ago

Hi, I need help changing the hair on my character to super Saiyan hair but, it uses a StarterCharacter in StarterPlayer and AddAsscessory() Doesn't seem to work so can anyone help here is my script btw

local p = game.Players.LocalPlayer --Get the localplayer
local char = p.Character


function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.X then
         local part = Instance.new("Part", workspace)
  part.CanCollide = false
  part.Anchored = true
  part.Position = p.Character.HumanoidRootPart.Position 
  part.BrickColor = BrickColor.new("New Yeller")
  part.Shape = "Ball"  
  part.formFactor = 'Symmetric'
 part.TopSurface = 0
  part.BottomSurface = 0
  part.Size = Vector3.new(1, 1, 1)
 wait (0.01)
part.Size = Vector3.new(3, 3, 3)
 wait (0.01)
part.Size = Vector3.new(5, 5, 5)
 wait (0.01)
part.Size = Vector3.new(7, 7, 7)
 wait (0.01)
part.Size = Vector3.new(9, 9, 9)
 wait (0.01)
part.Size = Vector3.new(10, 10, 10)
 wait (0.01)
   part.Transparency = part.Transparency + 0.1 
    wait (0.05)
    part.Transparency = part.Transparency + 0.1
    wait (0.05)
    part.Transparency = part.Transparency + 0.1
    wait (0.05)
    part.Transparency = part.Transparency + 0.1 
    wait (0.05)
    part.Transparency = part.Transparency + 0.1 
    wait (0.05)
    part.Transparency = part.Transparency + 0.1 
    wait (0.05)
    part.Transparency = part.Transparency + 0.1
    wait (0.05)
    part.Transparency = part.Transparency + 0.1
    wait (0.05)
    part.Transparency = part.Transparency + 0.1 
    wait (0.05)
    part.Transparency = part.Transparency + 0.1 
    wait (0.05)



    end

end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

2 answers

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

You need to weld the part you just created to the characters head.

local weld = Instance.new("WeldConstraint")
weld.Parent = game.Workspace
weld.Part0 = part
weld.Part1 = char.Head

local offset = Vector3.new(0,0,0)
part.Position = char.Head.Position + offset   -- the vector you're adding is the offset from the head
Ad
Log in to vote
0
Answered by 4 years ago

I encountered this problem earlier today. When I discovered that Humanoid:AddAccessory() didn't work in local scripts, I threw in the towel and stole some code, and put it in a modulescript.

ModuleScript:

mod = {}

function mod(character, accoutrement)

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

end

return mod

LocalScript:

require(ModuleScript)(character, accessory)

If you put the module in-game, you can use the local script at any point to add a hat to anything, locally.

I really wish I still had somebody to credit for this, but I no longer know where it came from. Hope this helps.

Answer this question