Utilizing for loops to set to text

local function SetupGui(options)
	for _, button in pairs(Choices:GetChildren()) do
		if button:IsA('ImageButton') then
			for i, v in pairs(options) do
				button.Map.Text = i
				button.Gamemode.Text = v
				
				break
			end
		end
	end
end

Problem is, I have 3 ‘buttons’ and all 3 are being set to the same map and gamemode. I know why this is happening, because everytime I loop through option it just picks the first one (whatever that may be) instead of actually setting each button to the different options, but I don’t know how to prevent it

1 Like

What appears to be happening, I believe, is that for each button, the for loop checking options finds the first pair, sets the button to have the values listed, and then breaks the loop.

Perhaps you can use table.remove on the options table to make sure gamemodes aren’t repeated?

That’s what I thought could work, but I’d need to get the index of the option chosen to remove it and I don’t know how to find that

maybe instead of going through all the options for each button, you can go through each option and set the text of its corresponding button. so it would look like this:

local function SetupGui(options)
    local buttons = {}
    local currentButton = 1
    for _, button in ipairs(Choices:GetChildren()) do -- get all the buttons in Choices
        if button:IsA("ImageButton") then
            table.insert(buttons, button)
        end
    end

    for map, gamemode in pairs(options) do -- go thru each option and set the text of corresponding button
        local button = buttons[currentButton]
        button.Map.Text = map
        button.Gamemode.Text = gamemode
        
        currentButton = currentButton + 1
    end
end

Your main issue here is that you’re treating it as an array but it is setup as a dictionary. For example, you cannot call table.remove() on a dictionary because it requires numeric indices, while a dictionary doesn’t (necessarily) have that.

The easiest approach to this issue would be to restructure your options dictionary into an array, and then you can utilize the index of each button to get the right option.

local Options = {
    { Map = "String1", Gamemode = "String2" },
    { Map = "String3", Gamemode = "String4" }
    { Map = "String5", Gamemode = "String6" }
}

for index, button in pairs(Choices:GetChildren()) do
    if button:IsA('ImageButton') then
        button.Map.Text = Options[index].Map
        button.Gamemode.Text = Options[index].Gamemode
    end
end

If restructuring your options array is not an option, you can loop over the options first and find the corresponding button instead:

local function SetupGui(options)
    local buttons = Choices:GetChildren()
    local index = 0
    for i, v in pairs(options) do
        index = index + 1
        local button =  buttons[index]
	    button.Map.Text = i
		button.Gamemode.Text = v
	end
end

Note that there are some edge cases here that neither of my solutions will account for, such as if there aren’t three options or if there are more than three options. I’m just giving you the tools to deal with your primary issue. :slight_smile: