Need help optimizing this code

Hello!

So i’m making an Avatar Customizer System, but I need help optimizing this code. Basically I want to make that arrow system that allows you to switch trough shirts.


This is my code :

Arrow1.MouseButton1Click:Connect(function()
       Arrow1.Visible = false
       Arrow2.Visible = true -- Arrow2 is to go back
       Arrow3.Visible = true -- Arrow3 is to go to next shirt
       Arrow4.Visible = false
       Arrow5.Visible = false
       Arrow6.Visible = false
       Arrow7.Visible = false
end)

Question :
Is there a way I can optimizing this code? (Without doing this manually)
Because it takes way too much time to copy that line everytime and setting the Visible variable to false/true.


Example :
image

2 Likes

Does this mean when Arrow3 is pressed, Arrow4 becomes visible and Arrow2 turns invisble? Can you show us the arrows location on the UI or is it many arrows layered on eachother?

1 Like

Exactly.

Here are some more examples :

if arrow1 is pressed then arrow2 and arrow3 are visible (Go to 2 next arrows)
if arrow2 is pressed then go back to arrow1
if arrow3 is pressed then arrow4 and arrow5 are visible (Go to 2 next arrows)
if arrow4 is pressed then arrow2 and arrow3 are visible (Go to 2 previous arrows)

Hopefully this is a bit more clear.


Edit: Arrow1 is the right arrow because you cannot go to the left since it’s the first arrow.

maybe this code will help point you in the correct direction

local shirts = {"Cat", "Red", "Long", "Tango"}

-- shirt 1 will be the default shirt
local selectedShirt = 1

local function UpdateGui()
    -- use the selected shirt to update the gui
    script.Parent.TextLable.Text = shirts[selectedShirt]
end

script.Parent.LeftArrow.Activated:Connect(function()
    -- deduct 1 from the selected shirt
    selectedShirt -= 1
    -- if the selected shirt is less then 1 then loop to the last shirt in the shirts table
    if selectedShirt < 1 then selectedShirt = #shirts end
    -- call the update gui function
    UpdateGui()
end)

script.Parent.RightArrow.Activated:Connect(function()
    -- increment the selected shirt
    selectedShirt += 1
    -- if the selected shirt is higher then the amount of shirts are table has loop back to 1
    if selectedShirt > #shirts then selectedShirt = 1 end
    -- call the update gui function
    UpdateGui()
end)

-- update the gui when the script first loads
UpdateGui()
2 Likes

Seems like what I need.
But the problem is that how can I fire a remote on each switch?

What I mean by that is, if the shirtName is “Cat”, it fires the remote to put on the cat face on your character.

You can simply make a remote called UpdateShirt and send the name of the shirt when you call the remote, like :FireServer(shirtName) while the server checks for what to do with the shirt name.

2 Likes
-- localsctipt
local shirts = {"Cat", "Red", "Long", "Tango"}

-- shirt 1 will be the default shirt
local selectedShirt = 1

-- set the gui to the default shirt
script.Parent.TextLable.Text = shirts[selectedShirt]

local function Update()
    -- send a event to the server telling it what shirt i have selected
    remoteEvent:FireServer(shirts[selectedShirt])
    -- use the selected shirt to update the gui
    script.Parent.TextLable.Text = shirts[selectedShirt]
end

script.Parent.LeftArrow.Activated:Connect(function()
    -- deduct 1 from the selected shirt
    selectedShirt -= 1
    -- if the selected shirt is less then 1 then loop to the last shirt in the shirts table
    if selectedShirt < 1 then selectedShirt = #shirts end
    -- call the update gui function
    Update()
end)

script.Parent.RightArrow.Activated:Connect(function()
    -- increment the selected shirt
    selectedShirt += 1
    -- if the selected shirt is higher then the amount of shirts are table has loop back to 1
    if selectedShirt > #shirts then selectedShirt = 1 end
    -- call the update gui function
    Update()
end)

-- server script
remoteEvent.OnServerEvent:Connect(function(player, shirt)
    -- print what shirt the player has selected
    print(player, "has selected", shirt)
    -- change the player upper torso to the new shirt
    player.Character.Humanoid:ReplaceBodyPartR15(enum.BodyPartR15.UpperTorso, game.ServerStorage[shirt])
end)
2 Likes

Thank you very much! It worked perfectly as I wanted to.

1 Like