Trouble with pcalls

Hello devforum,

I have a GUI system where when you close it, I want all the elements inside of it’s visibility to be turned to false. I know you can simply disable the GUI itself, but I won’t go into why I’ve chosen not to do this.

To do this, I have grabbed all the elements of the GUI using GetDescendants;

image

Now, my GUI contains instances which do not have the property of “Visiblity” such as UIAspectRatioConstants & Folders. So I found a tutorial which showed how to combat this. I writ down the code and hit test only to find this message;

To make sure I hadn’t just written my code wrong, I copied and pasted exactly what the tutorial had;

image

And below is the part of the script which references the function

image

Even doing the example of what the tutorial had (a simple print message) the same error occurred. I thought pcalls were supposed to ignore errors and continue the function, correct? Correct me if I’m wrong since I’ve had little experience with pcalls.

That’s strange, I tested the same thing you did, and it seemed to work for me. Just a few simple things you could consider to help debug:

  • Are you sure the error is coming from that part of the code?
  • Try adding a delay before the loop
  • Try integrating the pcall directly into the loop
  • Restart Studio - sometimes I spend a while trying to figure something out and it turns out it’s just Studio being broken

If all doesn’t work and you are unable to find a solution, you could just check if the element is a Frame, TextLabel, TextButton, etc.

1 Like

Could you try checking if it is an TextLabel, TextButton or a Frame?

if v:IsA("TextLabel") then
-- Your code here
end
1 Like

Well, it’s strange how this isn’t working…
What you could try instead is checking if the descendant is a GuiObject
Like this:

for i, v in ipairs(sp:GetDescendants()) do
    if v:IsA("GuiObject") then
        v.Visible = true
    end
end

As far as I know, all GuiObjects have a Visible property.

2 Likes

No need for ipairs or pairs
Fairly sure UIAspectRatioConstraints fall under the GUIObject class, so you’re better off

  • isolating / exluding by name
  • isolating / excluding through folders

Okay, I have read all your replies and will respond to all of them just in case some other person sees this in the future. It is a rather strange occurance, isn’t it?


@ChickwensRule

Are you sure the error is coming from that part of the code?

Yup. As soon as I added that part and tested it everything went skee-whiff.

Try adding a delay before the loop

Appears not to have worked either. I do not believe this can be the source of the problem as studio would probably do that freezing thing and then say it is exhausted or whatever.

Try integrating the pcall directly into the loop

I actually did this at first which I should have mentioned but yet, same thing again. Sorry for not mentioning!

Restart Studio - sometimes I spend a while trying to figure something out and it turns out it’s just Studio being broken

Very unfortunately, studio was not broken this time. Would have been brilliant if it was though!


@ChickwensRule & @firasthe2

If all doesn’t work and you are unable to find a solution, you could just check if the element is a Frame , TextLabel , TextButton , etc.

Could you try checking if it is an TextLabel, TextButton or a Frame?

The ol’ faithful! I’m still learning so I try searching around for different ways to make my code more compact and efficient which is why I didn’t just list off all the different GUI types. If there was no other way, I would have done this. Still a valid solution.


@Xsticcy

Your solution seems to have worked! I thought something like this may have existed since you can do x:IsA("BasePart"), which is not a property displayed on the properties menu. Is there a list of these “higher-up” properties like BasePart and GuiObject anywhere on documentation?


Thanks for all your replies by the way, maybe at some point someone will find this and provide a solution including pcall, hehe.

1 Like

You’re welcome for the solution!
Unfortunately, I think there isn’t a list of these baseclasses right now.
Here are the ones (I think) you might need during everyday development though:

PVInstance -> Includes BaseParts and Models
BasePart -> Includes all types of parts (Part, WedgePart, TrussPart, etc...)
GuiObject -> Includes every Gui Object (Frames, TextButtons, TextLabels, etc...)

So far, I don’t think I’ve ever used any other base classes.


Btw, I don’t recommend using pcall in loops, it’s pretty slow and you should use :IsA instead whenever possible.

1 Like

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