Making a textbutton go to the next word on the table list on click

I am making a customization gui and in the localscript, I was wondering how could I make it so that when the player clicks the textbutton (HatBTN) it goes to the next word on the list?
I want to do this in order for it to know what hat the player wants to wear.


You could do something like this

-- this is pseudo code, it may error
local currentIndex = 1
hatBTN.MouseButton1Click:Connect(function()
    currentIndex += 1
    if currentIndex > #hatsAccessories then -- this is so that whenever you get past the last hat, it just goes back to the first option (None)
        currentIndex = 1
    end

    local newHat = hatsAccessories[currentIndex]
    print(newHat) -- output is the string of the current index hat from the table
    -- do stuff with the hat string
end

Basically what’s happening here is that we are defining a currentIndex variable that will represent the current number of the table we are on. For example, 1 would be "None", 2 would be "BlueClassicCap", and so on… Once the button is clicked, we will increase the index by one. If the index is greater than the amount of accessories you have in your table, we will reset it back to 1 ("None"). Now if you want to get the current hat, you will have to do hatsAccessories[currentIndex], and that will return your current hat’s string.

TLDR; It just iterates once through the table each time the player clicks the button :slight_smile:

I hope this helps.

2 Likes

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