Can you concatenate directories to instances? (See attached image)

image
This is code I have to flip between multiple pages in a GUI, and most of it seems to work! However, CurrPage.Visible = true does not seem to work, why is this, and is there a way to workaround this without if elseif spam?

To explai further, Page is an integer, PPage is the start of the variables for each page frame, look in the image you see lines such as PPage1.Visible = false
what I want to achieve is to concatenate “PPage” with the page number to have a very compact way to enable the visibility of the page selected with the page up and down buttons. Its only this part of the function that does not seem to work. I will heavily appreciate any help!

ps yes I have tried without concatenating with the variable, using script.parent.Page…Page, sadly that gives the same results

1 Like

You can simply do FindFirstChild(“PPage” … Page).

3 Likes

There’s also a way you can make the code a bit more concise. Instead of setting each page’s Visible property to false individually, you can loop through all of the pages with a for i,v in pairs(<parent of the pages>:GetChildren()) loop

Hope this makes it a little less tedious!
(and do try @Misinformater’s solution)

3 Likes

You can see PPage is underlined, and it should say global PPage is not defined because it isn’t a variable. Similarly to what @Misinformater said you can just do [parent here]:FindFirstChild("PPage"..Page) to get the page instance.

4 Likes

I was already considering doing this before I wrote this, but thanks for suggesting it anyway :>

2 Likes
CurrPage =  script.Parent:FindFirstChild("PPage" .. Page)

	CurrPage.Visible = true

image
it doesnt work, apparently it does not exist
Seems like concatenating to target an instance doesn’t work, might just have to deal with if elseif loops

1 Like

Can you print CurrPage after defining it? It could simply be trying to look for an object named PPage0 because of how you set up the Page variable

2 Likes

You need to use FindFirstChild with the name of the page rather than the old variable.

For example, if the page is named “Page1” (make sure to add the number in their name), use FindFirstChild("Page"..Page)

Also, make sure Page starts at 1.

2 Likes

Make sure you’re using the FindFirstChild function* in the object that holds the page you want to index.

2 Likes

facepalm moment, it works now, thanks so much to everyone’s help!

4 Likes