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

Why doesn't the hair attach to the NPC?

Asked by 2 years ago

This code is used in a character select menu to chose the hair you want on your character, but when the hair clones it just falls into the void.

local button = script.Parent


local function onButtonActivated()
    game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui')
    local hair = game.Lighting.Hair.Hair01:Clone()
    local char = game.Workspace.Intro.Char
    hair:clone().Parent = char
end

button.Activated:Connect(onButtonActivated)

1 answer

Log in to vote
1
Answered by
AronYstad 101
2 years ago
Edited 2 years ago

You need to attach the hair somehow. I haven't done this myself, but if char is a character with a Humanoid, this might work:

local function onButtonActivated()
    game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui')
    local hair = game.Lighting.Hair.Hair01:Clone()
    local hairClone = hair:Clone()
    local char = game.Workspace.Intro.Char
    hairClone.Parent = char
    char.Humanoid:AddAccessory(hairClone)
end

I'm not entirely sure why you would need to clone it twice though, so this could also work:

local function onButtonActivated()
    game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui')
    local hair = game.Lighting.Hair.Hair01:Clone()
    local char = game.Workspace.Intro.Char
    hair.Parent = char
    char.Humanoid:AddAccessory(hair)
end

Edit: It seems like you might not even need to set the hair's parent, since that seems to be included in the AddAccessory function.

Edit 2: Apparently you can't set its parent before using AddAccessory. Also, this needs to be done in a regular script, not a local script. So if you're using a local script, use a RemoteEvent.

Edit 3: Here is a local script that worked for me:

local Button = script.Parent

local function onButtonActivated()
    game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui')
    local hair = game.Lighting.Hair.Hair01:Clone()
    local char = game.Workspace.Intro.Char
    hair.Parent = char
    local weld = Instance.new("Weld")
    weld.Parent = hair.Handle
    weld.Part0 = hair.Handle
    weld.Part1 = char.Head
    weld.C0 = hair.Handle.CFrame
    weld.C1 = char.Head.CFrame
    hair.Handle.Position = hair.Handle.Position + Vector3.new(0,0.7,0)
end

Button.Activated:Connect(onButtonActivated)

I changed the position by 0.7, cause for some reason the hat I tested it with had to be offset that amount. Idk if you're supposed to change the position of the Handle or the Mesh though, but at least it attaches now.

0
Doesn't work for me iSidersz 70 — 2y
0
I'm pretty sure I figured out the problem. Do you have it in a local script or a regular script? Cause it only seems to work in local scripts. Also, apparenlty you cannot set the hair's parent before using AddAccessory, or else it will give an error. AronYstad 101 — 2y
0
Oops. I meant to say it only works in regular scripts. AronYstad 101 — 2y
0
Yeah it is in a local script iSidersz 70 — 2y
View all comments (3 more)
0
However, I only want the hair to show up for the person who clicks the button, so how do I go about doing that? iSidersz 70 — 2y
0
Then you can't use AddAccessory, but you could probably just create a weld. I'm gonna test it, and then edit my answer with the code that works. AronYstad 101 — 2y
0
It also seems like you could use weld.C1 = char.Head.CFrame + Vector3.new(0,0.7,0) to change the position instead. AronYstad 101 — 2y
Ad

Answer this question