Make Hair select random

So I made a script to copy hats from a specific folder but I want to make Hair be chosen randomly. This is my script:

function TransformModule.AddHair(Char)
	local Transformed = Char:FindFirstChild("Transformed")
	local PlayerType = Transformed.Type
	local Folder = script.Accesories
	for _, v in pairs (Folder:GetChildren())do
		if v.Name == PlayerType.value then
			if v:FindFirstChild("Hair") then
				local FClone = v:FindFirstChild("Hair")
				for _,v in ipairs(FClone:GetChildren()) do
					print(v)
					local Clone = v:Clone()
					Clone.Parent = Char
					Clone:FindFirstChild("Handle").Anchored = false
					local TransparencyInfo = TweenInfo.new(3)
					local Trantween = game:GetService("TweenService"):Create(Clone:FindFirstChild("Handle"),TransparencyInfo,{Transparency = 0})
					Trantween:Play()
					Clone:FindFirstChild("Handle").Color = Transformed:FindFirstChild("Color").Value

				end
			end
		end
	end
end

You could make an array with the hairs and pick a random number, like this:

local Hairs = {
    Hair1 = Folder.Hair1, --Index number 1
    Hair2 = Folder.Hair2, --Index number 2
    etc.
}

-- Or this

local Hairs = HairsFolder:GetChildren()

-- And then to pick a random one in the array:

local RandomlyPickedHair = Hairs[math.random(1,#Hairs)]
1 Like

How would I place it into the Player Char?

If the hair is an accesory, you can just parent them to the Char and i believe they should glue to the head. If you’re supersticious, you can also do Humanoid:AddAccesory(YourAcessoryHere)

Oh I did that but it wouldt go to the player but I forgot to apply it in the main script.

https://developer.roblox.com/en-us/api-reference/function/Humanoid/RemoveAccessories
https://developer.roblox.com/en-us/api-reference/function/Humanoid/AddAccessory

Are the two functions you should be making use of.