You can write your topic however you want, but you need to answer these questions:
I want to achieve that when I click a button, it goes back to the previous item rather than just stopping working.
The issue is that when I try to click the button back, it will not go to the previous item. https://imgur.com/kTaxj3L
Found no solutions so far
Hello there, when I try to click a button, it will not return to the previous item that came before the new item, and I am rather stuck on this problem
local index = 0
local value = Hair[1]
HairUI.Right.MouseButton1Click:Connect(function()
index = (index >= #Hair) and 1 or (index + 1)
value = Hair[index]
HairUI.Title.Text = value.Name
end)
HairUI.Left.MouseButton1Click:Connect(function()
index = (index <= #Hair) and 1 or (index - 1)
value = Hair[index]
HairUI.Title.Text = value.Name
end)
When going left or right, you always check if the index caps at the hair limit which is good so it doesn’t exceed it and instead cycles again to the first or last one.
The problem with this is that you specified if index is less than the maximum amount of hairs when going left instead of checking if it were greater than the minimum amount of hairs (1)
With that logic, if i had hair number 6 and there were 10 hairs, and I went left (6 - 1 = 5) it would check if index is less or equal to 10 (6 <= 10) and set it back to 1.
To fix this, if the hair amount is less than or equal to one, it should cycle back to the maximum amount of hairs.
Also, if you’re on the first hair, index as a variable should start at one. This replaced code should work for you:
local index = 1
local value = Hair[1]
HairUI.Right.MouseButton1Click:Connect(function()
index = (index >= #Hair) and 1 or (index + 1)
value = Hair[index]
HairUI.Title.Text = value.Name
end)
HairUI.Left.MouseButton1Click:Connect(function()
index = (index <= 1) and #Hair or (index - 1)
value = Hair[index]
HairUI.Title.Text = value.Name
end)
Ah, but the problem is that I need to get back the previous item like. If you have 1. Spikey and then 2 Chroma, then if you click Back two times it goes back to Chroma, but I can’t seem to find a proper way of doing it.
It would be: Spikey, Chroma then backwards Chroma, Spikey, Chroma
Try to check if you don’t have more “Items” to be returned. Well, make it return to the maximum value if it reaches the value less than the minimum (1).
local index = 0
local value = Hair[1]
HairUI.Right.MouseButton1Click:Connect(function()
index = (index >= #Hair) and 1 or (index + 1)
value = Hair[index]
HairUI.Title.Text = value.Name
end)
HairUI.Left.MouseButton1Click:Connect(function()
if index == 0 then
index = #Hair --["Hair" does it return all items?]
elseif index > 0 then
index = (index <= #Hair) and 1 or (index - 1)
end
value = Hair[index]
HairUI.Title.Text = value.Name
end)