Can't make parts inside a table visible/invisible

  1. What do you want to achieve?
    When a textbutton is clicked it should open another gui with imagebuttons, it did work when i typed every part of the table seperately like:
    buttontable[1]
    buttontable[2]
    …and so on but instead of doing that i know i can use table.concat to merge all the values inside a table together and make it look nice and clean. But then it stopped working, here’s the script:

The table.concat function is used to concatenate strings in a table, not to access properties of objects inside a table. You need to iterate through the table and set the Visible property of each object individually.

textButton.MouseButton1Click:Connect(function()
    textButton.Visible = false
    ImageLabel.Visible = false
    background.Visible = true
    for _, button in ipairs(buttontable) do
        button.Visible = true
    end
end)

buttontable[1].MouseButton1Click:Connect(function()
    for _, button in ipairs(buttontable) do
        button.Visible = false
    end
    background.Visible = false
    wait(0.5)
    humanoidRootPart.CFrame = part.CFrame
    currentcamera.CFrame = character.Head.CFrame
    currentcamera.CameraType = Enum.CameraType.Custom
end)

Thank you, it worked but could you explain what this line does:

1 Like

This is a for-loop that iterates over objects stored your buttontable table and sets the Visible of each button in the table to false. Let me know if you want a more in-depth explanation.

1 Like

I am really wondering about the: _, button and ipairs
so if im not being a bother could you explain what _, does and ipairs in depth

_ (underscore):

  • Think of _ as a placeholder. It’s like saying, “I don’t really care about the specific number or position of the button, I just want to do something with each one.”
  • So, when you see _, it’s like saying, “Okay, this part of the code is just doing something with each button, and I don’t need to worry about which button number we’re on.”

ipairs:

  • ipairs is like a special way of going through each button in order, starting from the first one and moving to the last.
  • It’s handy when you have a bunch of things (like buttons) listed one after the other in a table, and you want to do the same thing with each one.
  • So, ipairs helps you go through each button in order without missing any.

Putting it all together:

for _, button in ipairs(buttontable) do
    button.Visible = true
end

Here, ipairs is helping us go through each button in the buttontable, and _ is saying we don’t really care about the specific number of the button, we just want to make each one visible.

1 Like

Only _ is a placeholder or _, (underscore with the comma) is there a difference between them?

Is ipairs only used for looking through tables?

if i wanted to only make the first imagebutton in the buttontable invisible would the script look like this:

for _, button in ipairs(buttontable[1]) do
    button.Visible = true
end

Would _, button in this script only reference the first image button?

In what other cases could we use _, for

1 Like

The comma after the underscore is simply a convention and doesn’t affect its functionality. They both indicate that you’re not interested in the specific value being iterated over, only the values themselves.

ipairs is commonly used for iterating over arrays or tables with sequential integer keys. It’s particularly useful when you want to loop through a table in numerical order, such as when dealing with arrays.

If you want to make only the first image button in the buttontable invisible, you wouldn’t need to use a loop. Instead, you’d directly access the first element in the table like this:

buttontable[1].Visible = false

“1” refers to the index or position in the table. Whatever button is inserted first in your code will appear there. You can also set it manually, like buttontable[index] = blahblablah.

for index, button in ipairs(buttontable) do
    button.Visible = true
    print(index)
end

… would print the position the button is in your button table.

1 Like

Thanks, that was much of help, sorry again for bothering, you really didn’t have to answer all of these questions but you did so I really appreciate it.

1 Like

My pleasure! Seems like you are genuinely putting in effort to learn rather than be fed code, so I appreciate that too.

Best of luck! :smiley:

1 Like

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