I have made a character customisation system but it really long to do

Yo guys, so I just did a character customisation system but it’s a really long code and I need help making it shorter. So I got 20 hairstyles and I want to make it every time I click on the arrows GUIs change to the next hairstyle in order of the names/numbers in their names.

Here is how my GUIs are looking like :

Here is how I store my hairstyles :

image

and here is how I do it right now, but I think that it’s a bit to long and that we could do something about like looping or something else but I don’t know :

hair.ForwardArrow.MouseButton1Click:Connect(function()
	if player.Hair.Value == "Hair1" then
		player.Hair.Value = "Hair2"
	elseif player.Hair.Value == "Hair2" then
		player.Hair.Value = "Hair3"
	elseif player.Hair.Value == "Hair3" then
		player.Hair.Value = "Hair4"
	elseif player.Hair.Value == "Hair4" then
		player.Hair.Value = "Hair5"	
	elseif player.Hair.Value == "Hair5" then
		
	end
end)

If you need more info you can ask me about it on this post I’m really active.

Thank you to anyone that helps.

1 Like

You can store all of the hair values in a table, and store your current index of that table.

local hairValues = {"Hair1","Hair2",...}
local index = 1

hair.ForwardArrow.MouseButton1Click:Connect(function()
    if index < #hairValues then
        index = index + 1
    else
        index = 1
    end
    player.Hair.Value = hairValues[index]
end)
3 Likes

Thank you, this is exactly what I was looking for. I’m really grateful. :grin:

1 Like

Considering the way you named them in the explorer, I would do it like this

local val = 1
local hairs = game:GetService("ReplicatedStorage").Customisation.Hairs:GetChildren()

function assignHair(val)
    player.Hair.Value = "Hair"..tostring(val)
end

assignHair(val)

hair.ForwardArrow.MouseButton1Click:Connect(function()
    val = (val + 1) % #hairs
    assignHair(val)
end)
3 Likes

You are right, it would be faster than the one @CodeJared sent, thank you. But his worked as well. Thank you to both of you.

2 Likes

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