Script to change hair

Hello, I designed a small place to be able to test some hair that I was making to practice, but as a result of this I have a small problem:

local block = script.Parent
local characterAccesory = script.Parent.Parent.Rig
local playerOnFloor = false

block.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid and not playerOnFloor then
		playerOnFloor = true

		local character = humanoid.Parent

		for _, accessory in ipairs(character:GetChildren()) do
			if accessory:IsA("Accessory") and (string.find(accessory.Name:lower(), "hair") or string.find(accessory.Name:lower(), "Hair")) then
				accessory:Destroy()
			end
		end


		local accesorioRig = characterAccesory:FindFirstChildOfClass("Accessory") 
		if accesorioRig then
			local accesorioNuevo = accesorioRig:Clone()
			accesorioNuevo.Parent = character    
		else
			warn("El accesorio no se encontrĂł en el Rig.")
		end
	end
end)

block.TouchEnded:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then
		playerOnFloor = false
	end
end)

Regardless of whether the script can be improved or not, my problem is that what I do is add my hair to the character and take out any other hair I have, but I do that by looking for an accessory that has the name “hair” or " Hair", however, not all hairs contain that type of name, is there any way I can remove everything that has to do with hairs or things on the head without having to do it this way and that it can also fail?, thank you.

1 Like

By “Hair” do you mean actual hair, or a hat like a baseball cap? Because if it’s any accessory, you can just remove the hair name check and it clears all accessories. If you just want “Hair” as an AccessoryType, then Accessories have an AccessoryType property that you can search for hairs.

image

3 Likes

Great!, I didn’t know that. I’ll put it into practice later and reply to say what’s up, if I had any other problems, thanks!

Edit: I have already tried it and it has worked wonderfully, thank you very much.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.