Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help! Detect if NPC is wearing a Hair if yes, remove it!?

Hello, i need to do this code work:

if game.Workspace.NPC.Hair then
game.Workspace.NPC.Hair:Destroy()

The script need to detect if the NPC is using a Hair and if yes, delete it and if not... execute this:

if game.Players.LocalPlayer.Data.Hair.Value == 1 then
hair = game.ReplicatedStorage.Hair1:Clone()
hair.Name = "Hair"
hair.Parent = NPC

Full code:

local Sound = script.Parent.ClickSound
local Menu = script.Parent.Parent.Parent.HairChooser
local MenuAppearance = script.Parent.Parent.Parent.MenuAppearance
local NPC = game.Workspace.NPC
local cam = game.Workspace.CurrentCamera

function onButtonClicked()
if game.Workspace.NPC.Hair then
game.Workspace.NPC.Hair:Destroy()
end
Sound:Play()
wait(0.5)
game.Players.LocalPlayer.Data.Hair.Value = 1
wait(0.1)
if game.Players.LocalPlayer.Data.Hair.Value == 1 then
hair = game.ReplicatedStorage.Hair1:Clone()
hair.Name = "Hair"
hair.Parent = NPC
wait(0.1)
Menu.Visible = false
wait(0.1)
MenuAppearance.Visible = true
end end

script.Parent.MouseButton1Click:connect(onButtonClicked)

2 answers

✓ Accepted answer

You could look for HairAttachments inside of the accessories

for _,v in pairs(NPC:GetChildren()) do
	if v.ClassName == "Accessory" and v.Handle:FindFirstChild("HairAttachment") then
		v:Destroy()
	end
end
for i,v in pairs(game.Workspace.NPC:GetChildren()) do -- Iterates through the npc's children
	if v:IsA('Accessory') then -- if the child is an accessory (hat) then...
		v:Destroy() -- Destroy the hat
	end
end

However, I'm not sure how to make it destroy only hair. This would delete all of the NPC's accessories.

This discussion is preserved as it was. New answers, comments, and votes are closed.